tableHeaderView的自动布局 [英] Auto Layout for tableHeaderView

查看:203
本文介绍了tableHeaderView的自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过学习自动布局来改进我的布局技术,但我偶然发现了一个我无法解决的问题。

I've been trying to improve my layout techniques by learning Auto Layout, but I've stumbled across an issue that I just haven't been able to solve.

这是发生的事情:有一个视图控制器,其中包含 UITableView UIView 。此视图位于表格视图的正下方,其高度不会延伸到整个屏幕高度(假设它占据了它的上半部分)。

Here's what happens: there's a view controller containing, among other things, a UITableView and a UIView. This view is placed just below the table view, and its height does not extend to the full screen height (let's say it takes up the top half of it).

表格视图,使用分组的样式并放置在上述视图的正上方,具有半透明背景,因此您可以实际看到其下方视图的内容。但是,由于视图占据了屏幕的一半,我需要在此表视图中添加tableHeaderView(也是半透明的),以便在第一次加载视图控制器时清晰可见整个背景内容。

The table view, using a grouped style and placed just above the mentioned view, has a translucent background, so that you can actually see the contents of the view just below it. Since the view takes up half of the screen, though, I need to add a tableHeaderView (also translucent) to this table view in order for the whole background content to be clearly visible when first loading the view controller.

这样的最终效果就像一个固定的tableHeaderView,当向上滚动表格视图时会在背景上消失。

The final effect of this would be like a fixed tableHeaderView that disappears on the background when scrolling up the table view.

这样做以一种简单的方式使用弹簧和支柱,但每次我尝试将标题添加到表视图时,Auto Layout都会崩溃应用程序(无论我怎么做)。

This is done in a simple way using springs and struts, but Auto Layout crashes the app every time I try to add the header to the table view (no matter how I do it).

由于我正在寻找一种方法来始终将背景中的视图高度(otherView)与tableHeaderView高度相匹配,我正在使用:

Since I'm looking for a way to always match the view height in the background (otherView) with the tableHeaderView height, I'm using:

[self.view addConstraint:[NSLayoutConstraints constraintWithItem:headerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:otherView attribute:NSLayoutAttributeHeight multiplier: 1 constant:0]];

(我的理解是约束不能直接添加到表视图中,因为otherView不是它的孩子)

(it is my understanding that the constraint can't be added directly to the table view, since otherView is not a child of it)

还有:

[headerView setTranslatesAutoResizingMaskIntoConstraints:NO]

这些调用总是导致这次崩溃:

These calls always result in this crash:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'

那么有什么方法可以做到这一点吗?是否可以使用自动布局将tableHeaderView高度实际绑定到视图上的另一个元素?

So is there any way to do this? Is it possible to actually tie a tableHeaderView height to another element on the view using Auto Layout?

我还应该注意到我已经远离IB,所以这所有都完全在代码中完成,特别是在 loadView

I should also note that I've long moved away from IB, so this all done purely in code, specifically on loadView.

推荐答案

如果你为NSLayoutConstraint创建一个实例变量并设置如下:

If you make a instance variable for the NSLayoutConstraint and set it like so:

headerViewHeightConstraint = [NSLayoutConstraint constraintWithItem:headerView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0f
                                                           constant:otherView.frame.size.height];

您可以通过以下方式调整大小:

You can resize it by:

headerViewHeightConstraint.constant = 1000.0f;
[headerView updateConstraintsIfNeeded];






PS:请记住 addConstraints: addConstraint:不同。

addConstraints 与约束数组一起使用或 [NSLayoutConstraints constraintsWithVisualFormat:...]

addConstraints is used in conjunction with an array of constraints or [NSLayoutConstraints constraintsWithVisualFormat:...]

addConstraint:与单个约束一起使用,这是上面的示例。

addConstraint: is used in conjunction with a single constraint which is the example above.

如果你要覆盖 layoutSubviews:在第一行中调用 [super layoutSubviews] 。这会强制视图及其子视图自行布局,以便您可以访问其框架详细信息等。

If you're overriding layoutSubviews: call [super layoutSubviews] in its very first line. That forces the view and its subviews to layout themselves so you can access their frame details etc.

这篇关于tableHeaderView的自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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