自动布局问题:iOS的7 VS iOS8上 [英] Auto-Layout Issues: iOS 7 vs iOS8

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

问题描述

我想创建一个可折叠的工具栏是这样的(iOS中7上运行 - 丑陋的颜色等,为可视化的目的):

I am trying to create a collapsible toolbar that works like this (running in iOS 7 -- ugly colors etc. for visualization purposes):

然而,当我运行的iOS 8 code,这是发生了什么:

我已成立是基于以下一个约束系统:

However, when I run the code in iOS 8, this is what happens: I have set up a constraint system that is based on the following:

A 定心视图(未显示)保持在画面中央的工具栏。
A 尺寸视图调整到崩溃的工具栏。在尺寸视图被固定在中心视图右侧(通过结尾约束)。
A 容器视图保存工具栏的实际内容。它被固定在右视图大小(也可通过一个结尾约束)。
各种内容的看法包含在容器视图。它们没有限制。该系统采用的默认约束应该是宽度,高度,顶部,左侧,从而确保他们保持在容器视图

A centering view (not shown) keeps the toolbar in the screen middle. A sizing view is adjusted to collapse the toolbar. The sizing view is anchored to the right of the centering view(via a trailing constraint). A container view holds the actual content of the toolbar. It is anchored to the right of the sizing view (also via a trailing constraint). Various content views are contained in the container view. They have no constraints. The default constraints applied by the system should be width, height, top, left, which ensures that they keep their relative positions in the container view.

工具栏坍塌实现如下:

- (IBAction)showLess:(id)sender {
    self.widthConstraint.constant = 50; // adjust this number for collapse / expand
    [UIView animateWithDuration:0.3 animations:^{
        [self.centeringView layoutIfNeeded]; // trigger animation
    }];
}

其中调整尺寸视图的宽度

问题:
iOS的8似乎表现得好像我已经离开固定内容的看法,但事实却并非如此。

Problem: iOS 8 seems to behave as if I had left anchored the content view, but this is not true.

我衷心AP preciate:

I would sincerely appreciate:


  • ,为什么iOS版的解释8将具有给定的(相当简单)的限制,例如一种完全不同的跨pretation。

  • 的指针,我怎么能在iOS 8
  • 预期的行为

来源$ C ​​$ C可这里(更新版本的iOS中的8部作品)。

Source code available here (updated version that works in iOS 8).

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

UPDATE: 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
    }];
}

我希望这可以帮助其他人谁也停留在此向前兼容性问题。

I hope this helps others who are also stuck on this forward compatibility issue.

推荐答案

这个问题是从堆栈溢出的答案解决。基本上,正确的答案是这个,但它很好地总结了的这个答案。 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.

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

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