自从我发现AutoLayout
我到处都使用它,现在我尝试将它与tableHeaderView
.
我制造了一个subclass
of UIView
添加了我想要的所有内容(标签等...)及其约束,然后我添加了这个CustomView
to the UITableView
'tableHeaderView
.
一切都很好,除了UITableView
总是显示above the CustomView
, by above我的意思是CustomView
is under the UITableView
所以看不到!
似乎无论我做什么,height
of the UITableView
'tableHeaderView
is always0(宽度、x 和 y 也是如此)。
我的问题:是否有可能做到这一点无需手动设置框架 ?
EDIT :
The 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”,因为手动编写约束要花很长时间,并且对于一个约束来说需要太多行,但这些方法是不言自明的。
And the 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
,我认为CustomView的高度是由约束决定的UILabel
其中的“标题”。
EDIT 2:经过另一次调查后,似乎 CustomView 的高度和宽度计算正确,但 CustomView 的顶部仍然与 UITableView 的顶部处于同一水平,并且当我滚动时它们一起移动。