【问题标题】:strange space coming between UITableView header and first rowUITableView 标题和第一行之间出现奇怪的空间
【发布时间】:2016-05-08 03:01:18
【问题描述】:

编辑:

现在删除自动布局后,它仅适用于 iPhone 4,4s 等较小的设备,但不适用于 iPhone 6 iOS 9.1.2。如果我添加一个高度为 80 的空白部分标题,它可以在 iPhone 6 中使用。仍然想知道可能是什么问题?

原问题: 我创建了一个 UIView 子类并为它创建了一个笔尖。 我已将它分配给我的 tableHeaderView 但问题出在笔尖中,我的视图高度为 200 像素,在代码中我需要手动将高度设置为 300 像素。这是代码

//Create and add header
self.header = [[[NSBundle mainBundle] loadNibNamed:@"LeftMenuHeaderViewViewController" owner:nil options:nil] lastObject];


self.header.frame = CGRectMake(0, 0, 260, 300);
self.header.target = (id)self;
// self.header.backgroundColor = [UIColor blueColor];
self.tblvLeftPanel.tableHeaderView = self.header;

这里是截图

实际的样子

这些方法都试过了

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.hidden = YES;
return sectionHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.01f;
}

自动布局图片 表视图

标题视图(无约束)

2 更多截图 高度 200 像素

高度 300 像素

似乎问题出在自动布局上

我已经尝试过这段代码,它正在工作

 self.header = [[[NSBundle mainBundle] loadNibNamed:@"LeftMenuHeaderViewViewController" owner:nil options:nil] lastObject];

 self.tblvLeftPanel.translatesAutoresizingMaskIntoConstraints = YES;
 self.header.frame = CGRectMake(0, 0, 260, 200);
 self.header.target = (id)self;
 self.header.backgroundColor = [UIColor clearColor];
 self.tblvLeftPanel.tableHeaderView = self.header;

但是在控制台中收到警告我该如何解决这个问题?

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
 Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that  you don't understand, refer to the documentation for the UIView  property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7fca1ca3f9b0 UITableView:0x7fca1b8fa000.height == 580>",
    "<NSLayoutConstraint:0x7fca1c9aa8d0 UIView:0x7fca1c9aa290.bottom == UITableView:0x7fca1b8fa000.bottom>",
    "<NSLayoutConstraint:0x7fca1c9aa920 UITableView:0x7fca1b8fa000.top == UIView:0x7fca1c9aa290.top + 20>",
    "<NSLayoutConstraint:0x7fca1ca409e0 UIView:0x7fca1c9aa290.height == 568>"
)

Will attempt to recover by breaking constraint 

【问题讨论】:

  • 你是否开启了自动布局?
  • 是的,我正在使用尺码等级。我也在 ViewDidLoad self.automaticallyAdjustsScrollViewInsets = NO;
  • 那么这很可能是一个自动布局工件。你能提供你所做的所有 AL 设置吗?
  • 在 viewForHeaderInSection 方法中返回 nil
  • 返回 nil 不起作用

标签: ios objective-c uitableview margin


【解决方案1】:

是的,UiTableView 有标题空间,你可以通过拉伸或隐藏在导航栏下隐藏这个视图。它的大小通常为 64,因此您需要将其拉到顶部 64 以使其随心所欲。

【讨论】:

    【解决方案2】:

    上面的一组约束是不一致的: 1- 桌面底部 == 视图底部 2- 桌子高度 == 568 3- 视图高度 == 580 4-视图高度==桌子高度+ 20 这是永远无法实现的。 IMO 的最佳解决方案是消除其中一个约束。哪一项取决于您认为哪一项最不重要。

    【讨论】:

    • 我删除了所有约束并以编程方式设置框架。它现在可以工作了。我仍然想使用 Autolayout 那么我应该应用哪些约束?请发布示例图片。
    • 请先使用模拟器在多种屏幕尺寸上进行测试。这将帮助您确定您希望视图如何适应屏幕空间。
    • 对 cme​​ts 的限制迫使我添加第二个。你想让上面的 UIView 适应屏幕大小吗?如果是这样,则限制其相对于其超级视图的顶部/底部,而不是其高度。您想要固定高度,然后限制其高度以及顶部或底部,但不能同时限制两者。对 UIView 应用相同的逻辑到 tableview。
    • 你的意思是从顶部和Pin从左、下、右到0的垂直间距