且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动布局问题:iOS的7 VS iOS8上

更新时间:2023-02-03 09:04:01

这个问题是从堆栈溢出的答案解决。基本上,正确的答案是这个,但它很好地总结了的这个答案。 iOS7和iOS8上的区别并不在方式的约束是相互preTED,但在方式,更新命令通过视图层次流淌下来。当我在iOS的7首次实施的行为,我注意到,动画只会正常工作,如果我叫 layoutIfNeeded 尺寸视图的父视图(即在定心视图)。在iOS中7显然这种自动流淌下来视图层次结构。在iOS中8,这是不会的情况:你必须手动失效的限制因素与 setNeedsLayout 更改视图,然后更新的布局 layoutIfNeeded 。我在更新的code的解决方案是这样的:

The issue was solved with answers from Stack-overflow. Basically, the right answer is this, but it was nicely summarized in this answer. The difference between iOS7 and iOS8 is not in the way the constraints are interpreted, but in the way that update commands are trickled down through the view hierarchy. When I implemented the behavior first in iOS 7, I noticed that the animation would only work properly if I called layoutIfNeeded on the parent view of the sizing view (i.e. on centering view). In iOS 7 this apparently trickled down the view hierarchy automatically. In iOS 8, this is not the case: You have to manually invalidate the view whose constraints have changed with setNeedsLayout, and then update the layout with layoutIfNeeded. My solution in the updated code looks like this:

- (IBAction)showLess:(id)sender {
    self.widthConstraint.constant = 50;
    [self.sizingView setNeedsLayout]; // *** THIS LINE IS NECESSARY TO MAKE THINGS WORK IN iOS 8
    [UIView animateWithDuration:0.3 animations:^{
        [self.sizingView layoutIfNeeded]; // trigger animation
    }];
}

我已经更新的问题,包括了答案,但在应对@unmircea我张贴一个单独的答案,也是如此。

I've updated the question to include the answer, but in response to @unmircea I am posting a separate answer, as well.