向UITableVIew标题视图添加约束 [英] Adding constraint to a UITableVIew headerview

查看:201
本文介绍了向UITableVIew标题视图添加约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是新的约束。



我有一个nib文件包含多个视图作为兄弟姐妹。 ViewController的视图包含一个tableView,我有另一个视图将被添加到tableHeaderView(让我们称之为self.tableHeaderView)。



面对是我想根据某些条件调整self.tableHeaderView的大小。我添加了约束我的所有UI元素,我不能,无论什么原因,通过nib添加一个高度约束到self.tableHeaderView。



I尝试改变self.tableHeaderView的框架编程,但是没有效果,当我在模拟器中运行代码,这是有道理的,因为如果我使用自动布局,它应该忽略框架的变化。



我尝试以编程方式添加高度约束,但它崩溃。



这是我要添加高度约束的代码片



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

我得到的异常:
***在 - [UITableView layoutSublayersOfLayer:] ,/SourceCache/UIKit_Sim/UIKit-2903.2/UIView.m:8536






*由于未收到而终止应用程式exception'NSInternalInconsistencyException',reason:'执行-layoutSubviews后仍然需要自动布局。 UITableView的-layoutSubviews的实现需要调用super。'*



最糟糕的情况我将添加另一个兄弟视图与第二个高度并复制UI元素但我想避免这种情况。





编辑1:我有这个异常
self.topicHeaderView.translatesAutoresizingMaskIntoConstraints = NO;



如果我没有它,我得到这个


无法同时满足约束。
以下列表中的至少一个约束可能是您不想要的约束。尝试这样:(1)查看每个约束,并尝试找出你不期望的; (2)找到添加了不需要的约束或约束并修复它的代码。 (注意:如果你看到NSAutoresizingMaskLayoutConstraints,你不明白,请参阅UIView属性的文档翻译autoresizingMaskIntoConstraints)




将尝试通过断开约束来恢复



objc_exception_throw在调试器中捕获这个。
在列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能有帮助。



编辑:2
在4屏幕上, (红色背景覆盖整个tableHeaderView,正如我预期的)



在3.5英寸屏幕上,红色背景(应用于笔尖上)延伸到一定高度,即使我将高度设置为117.0f 。在tableHeaderView里面的UI元素显示正确)





底部的蓝色线是分隔线,蓝色边框位于tableHeaderView的周围。

解决方案

当您将视图作为页眉或页脚添加到表视图时,您不能对此视图使用约束,但只能在其中。此外,视图必须位于层次结构的顶部(如您所见),如果将它作为子视图移动到另一个视图,则会产生相同的错误。



您可以通过设置具有更改高度的相同框架,直接在代码中更改视图的高度。



请记住,在您重新分配标题之前,此更改不会生效:

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


So I'm new to the constraints.

I've a nib file which contains multiple Views as siblings. 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).

The problem i'm facing is that I want to resize the self.tableHeaderView based on certain conditions. 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.

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.

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


* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call 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.

Edit 1: I get that exception when i've this 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; (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) ( "", "" )

Will attempt to recover by breaking constraint

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Edit: 2 On a 4" screen, it looks fine (The red background covers the entire tableHeaderView as i would expect)

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)

The blue line at the bottom is the separator line and the blue border is around the 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;

这篇关于向UITableVIew标题视图添加约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆