问题描述
我有让我以自己修改第二个约束后,适当布置的麻烦(第一个正常工作)。我修改一个约束,动画,然后在修改完成块(不包括动画),另一个制约因素。
I'm having trouble getting my view to lay itself out properly after modifying a second constraint (the first works fine). I'm modifying one constraint, animating it, and then modifying another constraint in the completion block (without animation).
我这样做的原因是我不希望第二个制约因素变化影响的动画,但我想作为初始动画完成它便可立即生效。
My reason for doing this is I don't want the second constraint change to affect the animation, but I want it to instantly take effect as soon as the initial animation completes.
问题是第二个约束的变化根本就没有发生作用,不管是什么我试试。
The problem is the second constraint change is simply not taking effect, no matter what I try.
下面就是我想要(注意这个问题,只有当 toolbarVisible
是是
,因为我执行这两个动画前面当它是 NO
)
Here's what I'm trying (note the problem is only when toolbarVisible
is YES
, as I perform both animations up front when it is NO
):
if ( !toolbarVisible )
self.containerViewBottomSpace.constant = toolbarVisible ? 60.f : 0;
self.toolbarBottomSpace.constant = toolbarVisible ? 0 : 60.f; // this one works fine
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:0.3f animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
if ( toolbarVisible )
self.containerViewBottomSpace.constant = 60.f; // this one is not taking effect
[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];
}];
以上方法需要一个 observeValueForKeyPath里面的地方:...
方法。我已经试过包装整个以上在 dispatch_after
块code,但有在造成更坏影响总是落后一步(即containerView总是在相反的位置,因为它应该是),如果第二个约束修改未生效,直到的下次的动画。
The above method takes place inside an observeValueForKeyPath:...
method. I've tried wrapping the whole above code in a dispatch_after
block, but that has an even worse effect in that it is always one step behind (i.e. containerView is always in the opposite position as it should be) as if the second constraint modification isn't taking effect until the next animation.
同样,如果我把另一个视图到导航控制器(嵌入在里面containerView),布局是否自行解决。这是因为虽然它只是无视我的命令本身又躺下了因某种原因。
Similarly, if I push another view onto the navigation controller (which is embedded inside the containerView), the layout corrects itself. It's as though it's just ignoring my command to lay itself out again for some reason.
其实我刚才注意到,即使我做了第二次变革开始和完全跳过建成块,它不会出现被动画在所有情况下第二个约束的变化。事情是腥。
Actually I just noticed that even if I make the second change initially and skip the completion block altogether, it doesn't appear to be animating the second constraint change in all cases. Something is fishy.
更新:我用 dispatch_after
错误地以为它接受了NSTimeInterval。给它一个合适的 dispatch_time_t
键,把它建成块内,布局是现在的有时的更新之后。然而,这是非常片状,这似乎很hackish的,所以我觉得好像我失去了一些东西。如果有人知道更多关于此请赐教,我给你答案。
Update: I was using dispatch_after
incorrectly thinking it accepted an NSTimeInterval. After giving it a proper dispatch_time_t
and putting it inside the completion block, the layout is now sometimes updating. However, it's very flaky and this seems very hackish so I feel as though I'm missing something. If anyone knows more about this please enlighten me and I'll give you the answer.
更新2:我决定尝试登录我想在 viewDidLayoutSubviews
来改变元素的高度。有趣的是,它被称为两次,第一次用previous值和第二的正确调整高度。所以我想它至少的部分的出正确铺设的事情,但由于某些原因的tableView那就是containerView内部仍不能正常调整本身。我不知道我错过了什么。
Update 2: I decided to try logging the height of the element I am trying to change in viewDidLayoutSubviews
. Interestingly, it gets called twice, first with the previous value and second with the correctly adjusted height. So I guess it is at least partially laying things out correctly, but for some reason the tableView that is inside the containerView is still not resizing itself properly. I'm not sure what I'm missing.
推荐答案
我倒是第一个断点,并确保你不打动画持续时间内动画code第二次。如果它在observeValueForKeyPath您可以将动画多次击中,它会导致一些不一致的地方。
I'd first breakpoint and make sure you're not hitting the animation code a second time during the animation duration. If it's in observeValueForKeyPath you could hit the animation multiple times and it causes some inconsistency.
您还应该不需要调用setNeedsUpdateConstraints或layoutIfNeeded。设置常数应该够了。
You also shouldn't need to call setNeedsUpdateConstraints or layoutIfNeeded. Setting the constant should be enough.
我也想确保你已经得到了约束接线正确,toolbarVisible是真实的。
I'd also make sure that you've got the constraints wired up correctly and toolbarVisible is true.
这篇关于当自动布局约束修改编程视图不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!