ios - 如何以更好的方式以编程方式布局 UI 元素?
<p><p>我正在以编程方式布置用户界面,如下面的代码。有没有更好的方法以编程方式做到这一点?例如使用设计模式或我什至不知道它存在的东西。
我正在寻找更好的方法的原因是,当我必须更改 UI 设计或布局时,感觉真的很丑陋和凌乱。</p>
<pre><code>- (void) loadView
{
;
self.view.backgroundColor = ;
self.title = @"ペットショップ";
float y = 44;
float x = 0;
float width = self.view.frame.size.width;
float height = width * .6;
if (!topPictureView_) {
topPictureView_ = [ initWithFrame:CGRectMake(x, y, width, height)];
topPictureView_.backgroundColor = ;
topPictureView_.image = ;
topPictureView_.contentMode = UIViewContentModeScaleAspectFill;
;
;
}
y+=height-35;
height = 35;
if (!lblBranchName_) {
lblBranchName_ = [ initWithFrame:CGRectMake(x, y, width, height)];
lblBranchName_.textColor = ;
lblBranchName_.backgroundColor = ;
lblBranchName_.textAlignment = NSTextAlignmentCenter;
lblBranchName_.text = @"ペットショップ KOMATSU";
lblBranchName_.font = ;
;
}
y+=height;
height = 45*4;
y+=5;
height = 50;
float col = self.view.frame.size.width/2;
float margin = 5;
btn11_ = ];
btn12_ = ];
y+= height+margin;
btn21_ = ];
btn22_ = ];
y+= height+margin;
btnChat = ;
y+=height+margin;
height = self.view.frame.size.height-y;
if (!eventBanner_) {
eventBanner_ = [ initWithFrame:CGRectMake(x, y, width, height )];
eventBanner_.image = ;
;
}
}
- (UIButton *) createButtonWithFrame:(CGRect) frame title:(NSString *)title image:(UIImage *) image {
UIButton *btn = [ initWithFrame:frame];
;
forState:UIControlStateNormal];
;
];
] forState:UIControlStateSelected];
[setBorderWidth:1.0f];
[ setBorderColor:.CGColor];
[ setCornerRadius:10.0f];
btn.clipsToBounds = YES;
;
return btn;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你应该使用 <a href="http://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html" rel="noreferrer noopener nofollow">NSLayoutContraint</a> .它为您的 UI 设计提供了更清洁和可移植的代码。如果您是新手,Ray Wenderlish 提供了一个不错的两部分 <a href="http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2" rel="noreferrer noopener nofollow">tutorial</a> .</p></p>
<p style="font-size: 20px;">关于ios - 如何以更好的方式以编程方式布局 UI 元素?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18759230/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18759230/
</a>
</p>
页:
[1]