它似乎假设您使用框架而不是自动布局,因为您只能使用框架进行真正的初始化。我这样做错了吗?
最佳答案
您可以通过编程方式使用自动布局。 您不需要使用框架初始化 UITableView。相反,您必须设置translatesAutoresizingMaskIntoConstraints = NO,将 TableView 添加到父 View ,然后在代码中定义约束。 例如:
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *views =
NSDictionaryOfVariableBindings(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]];
请引用https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AdoptingAutoLayout/AdoptingAutoLayout.html了解有关视觉格式的更多信息。您还可以选择不使用视觉格式并以编程方式描述约束。
关于objective-c - 如何使用自动布局以编程方式创建 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26837201/