【发布时间】:2013-05-04 12:33:42
【问题描述】:
自从我发现AutoLayout 之后,我到处都在使用它,现在我正在尝试将它与tableHeaderView 一起使用。
我在UIView 中添加了一个subclass,添加了我想要的所有内容(标签等...),然后我将这个CustomView 添加到UITableView'tableHeaderView。
除了UITableView 总是显示上方 CustomView 之外,一切正常,上方 我的意思是CustomView 在下方 UITableView 所以看不到!
似乎无论我做什么,UITableView'tableHeaderView 的height 都是总是 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上设置一些约束,我觉得CustomView的高度是由里面UILabel“title”上的约束决定的。
编辑 2: 经过另一次调查,似乎正确计算了 CustomView 的高度和宽度,但 CustomView 的顶部仍然与 UITableView 的顶部处于同一水平并且它们移动当我滚动时一起。
【问题讨论】:
-
是的,有可能。你能显示你正在使用的代码吗?如果不知道您在标题视图上设置了哪些约束,很难给出建议。
-
实现此目的的一种简单方法是将 IB 中的视图添加到 tableView..只需在包含 tableview 和 的同一场景中创建视图将其拖动到表格中。
-
我尽量避免使用 IB,到目前为止我还没有使用它,如果我不能让它工作我会尝试使用 IB
-
Apple 建议开发人员在自动布局方面尽可能使用 IB。它确实有助于避免很多不一致的问题。
-
真正完整的自动布局解决方案是here
标签: iphone ios ipad uitableview autolayout