自从我发现AutoLayout
我到处都在用它,现在我正试着把它用在tableHeaderView
上.
我做了一个subclass
页的UIView
页,添加了所有内容(标签等)我想知道他们的限制,然后我把这个CustomView
加到UITableView
'tableHeaderView
.
一切正常,除了UITableView
总是显示above和CustomView
,我的意思是CustomView
是under和UITableView
,所以它看不见!
似乎不管我怎么做,UITableView
‘tableHeaderView
的height
是always0(宽度、x和y也是).
我的问题是:到底有没有可能实现这without setting the frame manually个目标呢?
EDIT :
我使用的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",因为手动编写约束需要花费很长时间,而且一个约束的行数太多,但是这些方法是self 解释的.
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
个"title"的约束决定的.
EDIT 2:经过另一次调查后,CustomView的高度和宽度似乎计算正确,但CustomView的顶部仍然与UITableView的顶部处于同一水平,当我滚动时,它们会一起移动.