美文网首页
Bug:UITableViewHeaderView错位

Bug:UITableViewHeaderView错位

作者: 郭大猿 | 来源:发表于2017-05-15 18:20 被阅读105次

NO BB SHOW CODE

self.tableHeaderView = [[NSBundle mainBundle] loadNibNamed:@"XJPTradTodayHeaderView" owner:nil options:nil].firstObject;
self.tableView.tableHeaderView = self.tableHeaderView;
self.tableView.tableHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 254);
  1. 创建自定义 tableViewHeaderView
  2. 赋值给 tableViewtableViewHeaderView
  3. 设置 frame

问题:单纯如上设置会出现 tableViewHeaderView 的高度随不同 iPhone 模拟器而改变。如下图:

5323D9F6-16BC-4C01-9342-2CE8D9EDDD2E.png 1210359-d99b75101689757f.png

问题分析:联想 UICollectionView 也有类似的情况,为了不让系统自动调节控件高度,通常会在代码里添加如下代码:

self.automaticallyAdjustsScrollViewInsets = NO;

官方给的解释为:

66B5EEEC-D239-4748-A064-97BD09AA6072.png

大概意思为:automaticallyAdjustsScrollViewInsets根据按所在界面的status bar,navigationbar,与tabbar的高度,自动调整scrollview的 inset,设置为no,不让viewController调整。

联想:UITableView 是否也有类似属性。尝试打出 autoresizes ,惊喜发现自动提示 autoresizesSubviews ,点进去注释说明为:default is YES. if set, subviews are adjusted according to their autoresizingMask if self.bounds changes 。同样设置为 NO,再次在不同模拟器运行得到正确的布局。

解决:

self.tableView.autoresizesSubviews = NO;

相关文章

网友评论

      本文标题:Bug:UITableViewHeaderView错位

      本文链接:https://www.haomeiwen.com/subject/tgvhxxtx.html