我有一个应用程序,其视图是以编程方式生成的.例:
- -(void)loadView
- {
- [super loadView];
- // SET TOP LEFT BTN FOR NEXT VIEW
- UIBarButtonItem *topLeftBtn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
- self.navigationItem.backBarButtonItem = topLeftBtn;
- [topLeftBtn release];
- // programmatically set up the view for cart tableView
- CGRect IoUTableViewFrame = CGRectMake(0,320,348);
- IoUTableView = [[UITableView alloc]initWithFrame:IoUTableViewFrame style:UITableViewStylePlain];
- [[self IoUTableView] setDelegate:self];
- [[self IoUTableView] setDataSource:self];
- [[self view] addSubview:IoUTableView];
- // set up the summary label
- CGRect summaryTableFrame = CGRectMake(0,348,18);
- UILabel *summaryTableLabel = [[UILabel alloc] initWithFrame:summaryTableFrame];
- [summaryTableLabel setFont:[UIFont fontWithName:@"Helvetica" size:14]];
- [summaryTableLabel setText:@" Summary"];
- UIColor *labelColor = UIColorFromRGB(MiddleBlueColor);
- [summaryTableLabel setBackgroundColor:labelColor];
- [summaryTableLabel setTextColor:[UIColor whiteColor]];
- [[self view] addSubview:summaryTableLabel];
- // set up the summary table
- CGRect summaryTableViewFrame = CGRectMake(0,366,44);
- summaryTableView = [[UITableView alloc]initWithFrame:summaryTableViewFrame style:UITableViewStylePlain];
- [summaryTableView setScrollEnabled:NO];
- [[self summaryTableView] setDelegate:self];
- [[self summaryTableView] setDataSource:self];
- [[self view] addSubview:summaryTableView];
- }
是.我将在未来更新NIB并使用界面构建器和故事板,但我一年没有完成ios编程.
随着新的iPhone 5具有不同的屏幕尺寸,应用程序看起来不太好,我需要实现某种类型的自动布局.有没有办法以编程方式现在而不是使用IB?
非常感谢!
解决方法
是的,在NSLayoutConstraint中使用两种方法
- -(NSArray*)constraintsWithVisualFormat:options:metrics:views:
- -(NSLayoutConstraint*)constraintWithItem:attribute:relatedBy:toItem:attribute:
- multiplier:constant:
视觉格式语言全部打包成NSString
所以我以你的IoUTableView为例.
管道符号“|”代表着超级视野的优势.
[]代表一个视图.
那么我们在那里做了什么,我们将IoUTableView的左右边缘挂接到其超级视图的左右边缘.
视觉格式的另一个例子:
我们垂直钩你的表视图,摘要标签和汇总表.
现在,这三个视图在每个边缘垂直连接,NSLayoutFormatAlignAllLeft将所有视图对齐左侧,他们将基于其他约束,在这种情况下,先前的约束.
()用于指定视图的大小.
有更多的不平等和优先级以及“ – ”间隔符号,而是check out the apple docs for that
编辑:更正了使用constraintsWithVisualFormat的示例,如方法签名所示.