简体   繁体   English

将约束添加到UITableVIew标题视图

[英]Adding constraint to a UITableVIew headerview

So I'm new to the constraints. 所以我对限制很新。

I've a nib file which contains multiple Views as siblings. 我有一个包含多个视图作为兄弟的nib文件。 The ViewController's view contains a tableView and I've another view which is going to be added to the tableHeaderView (let's call it self.tableHeaderView). ViewController的视图包含一个tableView,我还有另一个视图,它将被添加到tableHeaderView(让我们称之为self.tableHeaderView)。

The problem i'm facing is that I want to resize the self.tableHeaderView based on certain conditions. 我面临的问题是我想根据某些条件调整self.tableHeaderView的大小。 I've added constraints to all of my UI elements and I can't, for whatever reason, add a height constraint to the self.tableHeaderView via the nib. 我已经为所有UI元素添加了约束,无论出于什么原因,我都不能通过nib向self.tableHeaderView添加高度约束。

I tried changing the frame of the self.tableHeaderView programmatically but that has no effect when i run the code in simulator, which makes sense because if I use Auto-layout, it should ignore the frame changes. 我尝试以编程方式更改self.tableHeaderView的框架,但是当我在模拟器中运行代码时没有任何效果,这是有道理的,因为如果我使用自动布局,它应该忽略框架更改。

I tried adding a height constraint programmatically but it crashes. 我尝试以编程方式添加高度约束但它崩溃了。

This is the piece of code I've to add the height constraint. 这是我要添加高度约束的一段代码。

    [self.tableHeaderView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableHeaderView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:107.0f]];

The exception i got: *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.2/UIView.m:8536 我得到的例外:***断言失败 - [UITableView layoutSublayersOfLayer:],/ SourceCache / UIKit_Sim / UIKit-2903.2 / UIView.m:8536


* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'执行-layoutSubviews后仍需要自动布局。 UITableView's implementation of -layoutSubviews needs to call super.'* UITableView的-layoutSubviews实现需要调用super。'*

Worst case scenario I'll add another sibling view with the second height and duplicate the UI elements but i want to avoid this. 最糟糕的情况我会添加第二个高度的另一个兄弟视图并复制UI元素,但我想避免这种情况。

Nib文件结构

Edit 1: I get that exception when i've this self.topicHeaderView.translatesAutoresizingMaskIntoConstraints = NO; 编辑1:当我有这个self.topicHeaderView.translatesAutoresizingMaskIntoConstraints = NO时,我得到了那个异常。

If i don't have it, i get this 如果我没有它,我会得到这个

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; 试试这个:(1)看看每个约束并试着找出你不期望的东西; (2) find the code that added the unwanted constraint or constraints and fix it. (2)找到添加了不需要的约束或约束的代码并修复它。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "" ) (注意:如果您看到了您不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)(“”,“”)

Will attempt to recover by breaking constraint 将尝试通过打破约束来恢复

Break on objc_exception_throw to catch this in the debugger. 打破objc_exception_throw以在调试器中捕获它。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助。

Edit: 2 On a 4" screen, it looks fine (The red background covers the entire tableHeaderView as i would expect) 编辑:2在4英寸的屏幕上,它看起来很好(红色背景覆盖整个tableHeaderView,如我所料) 4“模拟器

On a 3.5" screen, the red background (which is applied on the nib, extends to a certain height even though i set the height to be 117.0f. The UI elements that are inside the tableHeaderView shows up correctly) 在3.5英寸的屏幕上,红色背景(应用在笔尖上,即使我将高度设置为117.0f,也会延伸到一定高度。表格内部的UI元素正确显示)

3.5英寸的屏幕

The blue line at the bottom is the separator line and the blue border is around the tableHeaderView. 底部的蓝线是分隔线,蓝色边框位于tableHeaderView周围。

When you are adding a view as a header or footer to the table view, you cannot use constraints on this view, but only inside it. 将视图作为页眉或页脚添加到表视图时,不能在此视图上使用约束,而只能在其中使用约束。 Also the view must be on the top of the hierarchy (as you have), if you move it as a subview to another view, it will give the same error. 此外,视图必须位于层次结构的顶部(如您所示),如果将其作为子视图移动到另一个视图,则会产生相同的错误。

You can change view's height directly in code by setting the same frame with changed height. 您可以通过设置更改高度的相同框架直接在代码中更改视图的高度。 This is working fine. 这工作正常。

Also remember that this changes will not apply until you reassign the header: 另请注意,在重新分配标头之前,此更改将不适用:

tableView.tableHeaderView.frame = ...;
tableView.tableHeaderView = tableView.tableHeaderView;

I had the same issue, I couldn't use constraints on the tableHeaderView. 我有同样的问题,我无法在tableHeaderView上使用约束。

Then I created another subView to create the constraints into. 然后我创建了另一个子视图来创建约束。

// 1 - create a header view and a subHeaderView
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height - 64.f)];
headerView.backgroundColor = [UIColor blackColor];

tableView.tableHeaderView = headerView;

UIView *subHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height - 64.f)];
[headerView addSubview:subHeaderView];

// 2 - add constrainedView to subHeaderView (E.g.)
UIView *constrainedView = [UIView new];
[constrainedView setTranslatesAutoresizingMaskIntoConstraints:NO];
[subHeaderView addSubView:constrainedView];

// 3 - addConstraints of subviews into subHeaderView (E.g.)
[subHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[constrainedView]|" options:0 metrics:metrics views:views]];
[subHeaderView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[constrainedView]|" options:0 metrics:metrics views:views]];

This is working on my project, iOS 7.0 / Xcode 5.0.2 这适用于我的项目iOS 7.0 / Xcode 5.0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM