【发布时间】:2013-09-16 06:37:36
【问题描述】:
我正在代码中创建一个 UITableView,我想设置带有约束的大小 - 但没有显示 tableView:
_tableView = [[UITableView alloc] initWithFrame:CGRectZero];
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.autoresizingMask = UIViewAutoresizingNone;
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
NSDictionary *views = @{ @"tableView": self.tableView};
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[tableView]-10-|" options:0 metrics:nil views:views]];
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[tableView]-10-|" options:0 metrics:nil views:views]];
[self.view removeConstraints:self.view.constraints];
[self.view addConstraints:constraints];
只有没有约束,如果我使用initWithFrame:self.view.bounds,表格就会显示出来......我错过了什么?
编辑:如果我使用 self.view.bounds 初始化 tableView 将无济于事
编辑 2: 如果我删除我在 viewDidLoad 中也有的 self.view.translatesAutoResizingMaskIntoConstraints = NO;,一切似乎都很好 - 为什么不允许这样做?
【问题讨论】:
-
尝试初始化tableView Like : [UITableView alloc] init];
-
不会改变任何东西 :(
-
讨论 - 矩形被标准化,然后应用插入参数。如果生成的矩形的高度或宽度为负,则返回一个空矩形。
-
[self.view addSubview:_tableView];将 nil 框架添加到视图中。那有什么用呢..?
-
你在实例化数组
constraints吗?从代码中,数组将为 nil。即在添加约束后放置NSLog(@"%@", constraints);。我怀疑它会为零。
标签: ios objective-c uitableview autolayout