查看编程方式更改自动布局约束条件之后,没有更新 [英] View is not updating after changing auto-layout constraints programmatically

查看:140
本文介绍了查看编程方式更改自动布局约束条件之后,没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前我必须说,我其实得到了我想要的,但不是因为令人满意的方式的明显效果,现在,限制必须是破,而不是正确地更新。

Beforehand I have to say that I actually got the visible effect I've wanted but not in a satisfying way since, right now, constraints need to be "broken" instead of being properly updated.

我有一个的ViewController持有的UITableView。该的tableView 的高度可以从0(不可见)以任何其持有的行数是变化。高度在ViewController中的viewDidLoad()由行的高度行的当前数量乘以。

I got a ViewController which holds a UITableView. The height of that tableView can vary from 0 (not visible) to whatever the number of rows it holds is. The height is calculated in the ViewController's viewDidLoad() by multiplying the current number of rows by the row's height.

我试过的第一件事就是创建一个 @IBOutlet弱VAR tableHeightConstraint:NSLayoutConstraint 连接到故事板设定的高度约束。它只是一些随机的初始高度,但它更新到内的正确值的ViewController的 viewDidLoad中()随后实施方法,更新,如果需要的观点:

The first thing I've tried was creating an @IBOutlet weak var tableHeightConstraint: NSLayoutConstraint! connected to the height constraint set in the storyboard. It has just some random initial height but it is updated to the correct value within the ViewController's viewDidLoad() followed by method that updates the view if needed:

tableHeightConstraint = NSLayoutConstraint(item: tableView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: calculatedTableViewHeight)

// At this point I've tried a variety of methods like the following two but actually none has worked for me
view.layoutIfNeeded()

// or
view.updateConstraints()

// or
view.layoutSubviews()

......,甚至在的tableView同样的方法只是要确定,以及把所有这些在 viewDidLayoutSubviews()代替。

所以,我试图其次是创建相同的约束上面一样,但不是更新我只是把它添加到视图: view.addConstraint(tableHeightConstraint)。这实际上有所需的视觉效果,但在日志中我得到的导致初始一项的断这两个高度约束的冲突。由于我的目标是获得一个正确的和干净的code,我一直在尝试。所以这次我先删除了视图约束再次加入调整后的人之前。在视觉上,再次,一切都是完美的,但我还是没有摆脱冲突的约束。

So what I tried next was to create the same constraint as above but instead of updating I just added it to the view: view.addConstraint(tableHeightConstraint). This actually had the desired visual effect but in the logs I got a conflict of these two height constraints resulting in a break of the initial one. As my goal is to get a correct and clean code I've kept trying. So this time I first removed the constraint from the view before adding the adjusted one again. Visually, again, everything was perfect but I still didn't get rid of the conflicting constraints.

所以我实际的问题是(除了我在做什么错至今)我怎么能 - preferably - 只需更新现有的约束随后视图的适当更新/重新布局其在任何时候子视图,无论是这是我第一次加载观点还是我只想当用户进行交互,以改变一些限制。非常感谢您的帮助!

So my actual question is (besides what I was doing wrong so far) how I can - preferably - just update an existing constraint followed by a proper update/relayout of the view and its subviews at any point, whether it is when I first load a view or whether I just want to change some constraints when the user interacts. Thanks a lot for any help!

推荐答案

目前,你分配一个新的约束,而不是更新当前之一。新的约束也还没有被添加到视图层级

Currently, you're assigning a new constraint, instead of updating the current one. The new constraint also hasn't been added to the view hierarchy.

您使用一个IBOutlet引用您在IB建立了一个约束的做法是一个很好的办法,但不是到VAR分配一个新的约束,你只需要修改约束的常数:

Your approach of using an IBOutlet to reference a constraint you set up in IB is a good approach, but instead of assigning a new constraint to that var, you just need to modify the constraint's constant:

tableHeightConstraint.constant = calculatedTableHeight

您应该不需要来电来访layoutIfNeeded(),因为限制句柄上调用,当你改变自己的常量布局自动传递。 updateConstraints() layoutSubviews()绝对不是你想要的。

You shouldn't need to call layoutIfNeeded() here, since constraints handle calling a layout pass automatically when you change their constants. updateConstraints() and layoutSubviews() are definitely not what you want.

这篇关于查看编程方式更改自动布局约束条件之后,没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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