ios - 呈现 View Controller 后自动布局无效(以及许多情况)

标签 ios modalviewcontroller autolayout ios7

<分区>

背景:

我有一个自定义容器 Controller ,它就像 UITabBarController,但带有切换动画。而我是用auto layout来实现的。
因为子 Controller 之间的切换是动态的,所以当它的 View 被添加到容器的 View 中时,适当的约束被添加到 subview Controller ,而不是在 IB 中预先设置。 (约束当然是添加到父 View 中的)

http://d.pr/i/q6NF容器 Controller Nib 设置

附言: 约束的细节
H:|[Child](将约束的常量从左/右改为右/左动画)
H:[ child (== super )]
五:|[ child ]|

问题出在哪里:

其中一个子 Controller 是导航 Controller ,当导航 Controller 呈现模态视图 Controller (使用 presentViewController:animated:completion:)并关闭它(使用 dismissViewControllerAnimated:completion:), 在 present/dismiss navigation controller frame 变成 (x, y, 0, 0) 之后,似乎自动布局变得无效,可能是约束被移除了。

http://d.pr/i/VmvL出席/解雇流程

研究

我还没有使用代码来验证这些约束发生了什么,但是通过 Spark Inspector,我看到在呈现/关闭过程中 View 呈现发生了变化。当我的导航 Controller 呈现模态视图 Controller 时,iOS 只是将整个导航 Controller 的 View 交换到模态视图 Controller 的 View 。当导航 Controller 的 View 返回时,自动布局不再起作用。

可能的解决方案

我想到的解决方案之一是让我的容器 Controller 呈现模态 Controller 。

或者我只是将我的容器 Controller 更改为无自动布局。

事实上,自从我开始使用自动布局以来,这种技术造成的问题就占了优势。 除了这个问题,每次界面方向改变时,我的容器 Controller 内的 View 就不能正确自动布局,似乎 subview 总是在方向改变之前使用父 View 的框架。我仔细检查了我设置的约束,没有冲突也没有歧义。

我的猜测是我的自定义容器 Controller 与在自动布局系统中呈现模态视图 Controller 和界面方向更改不兼容,即使设置了约束也是如此。

Xcode 环境

Xcode 5 beta,iOS 7SDK,目标 iOS6.1可能是 SDK 环境不对?

最佳答案

我有一个 similar problem .我在我的根 UIView 上设置了 translatesAutoresizingMaskIntoConstraints = NO;。 它出现在“最外层”的 UIView - 层次结构根部的 super View 必须使用默认的 translatesAutoresizingMaskIntoConstraints = YES。 删除它后,一切都按预期工作。

关于ios - 呈现 View Controller 后自动布局无效(以及许多情况),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17666907/

相关文章:

iphone - 更改 UISegmentedControl 的布局?

ios - 如何从 CloudKit 容器中删除?

ios - 有没有办法限制标签的基线以匹配另一个 View 的底部边缘?

ios - "The view hierarchy is not prepared for the constraint" swift 3 错误

ios - CNContactVCardSerialization.dataWithContacts 给出异常

ios - 容器 View 中的 Split View失去导航 Controller 的功能

ios - 呈现模态 UIViewController 时发生奇怪的崩溃

iPhone - 作为模态 iOS > 5.0 的登录屏幕

ios - 在呈现模态视图时显示 UITabBar

ios - 带有 AutoLayout 的 UIScrollView 在 iOS 6 中完美运行,但在 iOS 7 中却不行