自从我发现了AutoLayout
,我在任何地方都使用它,现在我正在尝试将其与tableHeaderView
一起使用。
我创建了一个UIView
的subclass
,添加了所有我想要的东西(标签等),并设置了它们的约束条件,然后我将这个CustomView
添加到UITableView
的tableHeaderView
中。
一切都很正常,除了UITableView
总是显示在CustomView
的上方,我的意思是CustomView
在UITableView
下面,所以看不见!
无论我做什么,UITableView
的tableHeaderView
的高度始终为0(宽度、x和y也是如此)。
我的问题是:是否有可能不用手动设置框架就能完成这个?
编辑:
我使用的CustomView
的subview
具有以下约束条件:
_title = [[UILabel alloc]init];
_title.text = @"Title";
[self addSubview:_title];
[_title keep:[KeepTopInset rules:@[[KeepEqual must:5]]]]; // title has to stay at least 5 away from the supperview Top
[_title keep:[KeepRightInset rules:@[[KeepMin must:5]]]];
[_title keep:[KeepLeftInset rules:@[[KeepMin must:5]]]];
[_title keep:[KeepBottomInset rules:@[[KeepMin must:5]]]];
我正在使用一个方便的库“KeepLayout”,因为手动编写约束需要花费太长时间,而单个约束需要太多行,但这些方法是自解释的。
而UITableView
有这些约束:
_tableView = [[UITableView alloc]init];
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_tableView];
[_tableView keep:[KeepTopInset rules:@[[KeepEqual must:0]]]];// These 4 constraints make the UITableView stays 0 away from the superview top left right and bottom.
[_tableView keep:[KeepLeftInset rules:@[[KeepEqual must:0]]]];
[_tableView keep:[KeepRightInset rules:@[[KeepEqual must:0]]]];
[_tableView keep:[KeepBottomInset rules:@[[KeepEqual must:0]]]];
_detailsView = [[CustomView alloc]init];
_tableView.tableHeaderView = _detailsView;
我不确定是否需要直接在CustomView
上设置一些约束,我认为它的高度取决于其中UILabel
"title"上的约束。
编辑2: 经过另一番调查,似乎CustomView
的高度和宽度已正确计算,但其顶部仍与UITableView
的顶部处于同一水平线上,并且它们在滚动时一起移动。