当我以编程方式添加 UITableView 并使用自动布局时,我通常会这样编写代码:
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)
style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = @{@"tableView": self.tableView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"
options:0
metrics:nil
views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView|"
options:0
metrics:nil
views:views]];
UITableView 的默认初始化器需要一些 CGRect 来定位,但是当我们使用 AutoLayout 时,不需要以编程方式设置框架。
- 我是否正确添加了 UITableView?
- 有没有办法避免“虚拟”CGRect?
最佳答案
试试这个
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
关于ios - 以编程方式添加带有自动布局的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492797/