Ios 如何以编程方式更好地布局UI元素?
我以编程的方式设计用户界面,如下面的代码所示。有没有更好的方法以编程方式实现这一点?例如,使用设计模式或我甚至不知道它存在的东西。 我之所以要寻找更好的方法,是因为当我不得不改变UI设计或布局时,它会让我感觉非常难看和混乱Ios 如何以编程方式更好地布局UI元素?,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我以编程的方式设计用户界面,如下面的代码所示。有没有更好的方法以编程方式实现这一点?例如,使用设计模式或我甚至不知道它存在的东西。 我之所以要寻找更好的方法,是因为当我不得不改变UI设计或布局时,它会让我感觉非常难看和混乱 - (void) loadView { [super loadVIew]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"ペットショップ"; float
- (void) loadView
{
[super loadVIew];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"ペットショップ";
float y = 44;
float x = 0;
float width = self.view.frame.size.width;
float height = width * .6;
if (!topPictureView_) {
topPictureView_ = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
topPictureView_.backgroundColor = [UIColor lightGrayColor];
topPictureView_.image = [UIImage imageNamed:@"info_bg"];
topPictureView_.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:topPictureView_];
[self.view sendSubviewToBack:topPictureView_];
}
y+=height-35;
height = 35;
if (!lblBranchName_) {
lblBranchName_ = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height )];
lblBranchName_.textColor = [UIColor colorWithWhite:.3 alpha:1];
lblBranchName_.backgroundColor = [UIColor colorWithWhite:0.8 alpha:.7];
lblBranchName_.textAlignment = NSTextAlignmentCenter;
lblBranchName_.text = @"ペットショップ KOMATSU";
lblBranchName_.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
[self.view addSubview:lblBranchName_];
}
y+=height;
height = 45*4;
y+=5;
height = 50;
float col = self.view.frame.size.width/2;
float margin = 5;
btn11_ = [self createButtonWithFrame:CGRectMake(margin, y, col-margin*1.5, height) title:@"11" image:[UIImage imageNamed:@"menu"]];
btn12_ = [self createButtonWithFrame:CGRectMake(col+margin/2, y, col-margin*1.5, height) title:@"12" image:[UIImage imageNamed:@"order"]];
y+= height+margin;
btn21_ = [self createButtonWithFrame:CGRectMake(margin, y, col-margin*1.5, height) title:@"21" image:[UIImage imageNamed:@"order"]];
btn22_ = [self createButtonWithFrame:CGRectMake(col+margin/2, y, col-margin*1.5, height) title:@"22" image:[UIImage imageNamed:@"map"]];
y+= height+margin;
btnChat = [self createButtonWithFrame:CGRectMake(margin, y, col*2-margin*2, height) title:@"Chat" image:nil];
y+=height+margin;
height = self.view.frame.size.height-y;
if (!eventBanner_) {
eventBanner_ = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height )];
eventBanner_.image = [UIImage imageNamed:@"banner"];
[self.view addSubview:eventBanner_];
}
}
- (UIButton *) createButtonWithFrame:(CGRect) frame title:(NSString *)title image:(UIImage *) image {
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateSelected];
[[btn layer]setBorderWidth:1.0f ];
[[btn layer] setBorderColor:[UIColor grayColor].CGColor];
[[btn layer] setCornerRadius:10.0f];
btn.clipsToBounds = YES;
[self.view addSubview:btn];
return btn;
}
你应该使用。它为您的UI设计提供了更清晰、更可移植的代码。如果您是新手,Ray Wenderlish提供了一个由两部分组成的好方法。阅读有关AutoLayout的内容,还有为什么不试试Interface Builder呢。另外,不要从loadView调用[super loadView]。您可以重写此方法以手动创建视图。如果您选择这样做,请将视图层次结构的根视图指定给视图属性。您创建的视图应为唯一实例,不应与任何其他视图控制器对象共享。此方法的自定义实现不应调用super我被告知不要使用IB。我现在工作的公司就是这样做的。我试图不调用
[super loadView]
然后我的-(void)loadView
被无限调用。它在iOS 5中可用吗?是的。。事实上,这就是它被引入的地方。。b/c这时你开始得到不同的外形因素(即不同分辨率的屏幕等)。。这就是为什么需要有一种方法来描述ui元素在屏幕上出现的位置,而不依赖于屏幕大小/分辨率。。类似于android对其应用程序的描述layout@DulguunLst刚刚用一个简单易懂的教程更新了我的答案。。享受:)自动布局出现在iOS 6中。@StephenDarlington dang这是真的。。我的错