oky*_*eni 9 interface-builder uiview ios autolayout ios6
我有一个应用程序,其视图以编程方式生成.例:
-(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, 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, 320, 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, 320, 44);
summaryTableView = [[UITableView alloc]initWithFrame:summaryTableViewFrame style:UITableViewStylePlain];
[summaryTableView setScrollEnabled:NO];
[[self summaryTableView] setDelegate:self];
[[self summaryTableView] setDataSource:self];
[[self view] addSubview:summaryTableView];
}
Run Code Online (Sandbox Code Playgroud)
是.我将在未来更新到NIB并使用界面构建器和故事板,但我没有在一年内完成ios编程.
随着新的iPhone 5具有不同的屏幕尺寸,应用程序看起来不太好,我需要实现某种类型的自动布局.有没有办法以编程方式现在而不是使用IB?
非常感谢!
Dav*_*ong 31
是的,通过在NSLayoutConstraint中使用两个方法
-(NSArray*)constraintsWithVisualFormat:options:metrics:views:
-(NSLayoutConstraint*)constraintWithItem:attribute:relatedBy:toItem:attribute:
multiplier:constant:
Run Code Online (Sandbox Code Playgroud)
可视化格式语言全部打包成NSString所以我将以你的iouTableView为例.
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"|[iouTableView]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(iouTableView)]];
Run Code Online (Sandbox Code Playgroud)
管道符号"|" 代表超级视图的优势.[]代表一个视图.所以我们在那里做的是将iouTableView的左右边缘连接到其超视图的左右边缘.
可视化格式的另一个例子:让我们垂直挂钩你的表格视图,摘要标签和摘要表.
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
@"V:|[iouTableView(348)][summaryTableLabel(18)][summaryTableView(44)]"
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:NSDictionaryOfVariableBindings(iouTableView, summaryTableLabel, summaryTableView)]];
Run Code Online (Sandbox Code Playgroud)
现在,这将在每个边上垂直链接所有三个视图,NSLayoutFormatAlignAllLeft告诉所有视图左对齐,它们将基于其他约束(在本例中为前一个约束)执行此操作.()用于指定视图的大小.
有一点像不等式和优先级以及" - "间隔符号,但请查看苹果文档
编辑:更正了示例以使用constraintsWithVisualFormat,如方法签名中所示.
| 归档时间: |
|
| 查看次数: |
24215 次 |
| 最近记录: |