【发布时间】:2015-02-24 02:54:08
【问题描述】:
我有一个非常简单的Custom Container View Controller 可以在两个视图之间切换。添加内容视图(带有 .xib 的 UIViewController 包含一个带有 AutoLayout 约束的按钮)时,它的布局很好,没有冲突的约束。按下按钮将该视图交换为另一个视图(相同类型视图的另一个实例),该视图的布局也很好,没有冲突的约束。
当我再次交换视图以重新插入第一个视图(已存储并且与之前删除的视图相同)时,iOS“无法同时满足约束”。每次在第二次交换之后,iOS 都会抛出相同的约束不满足警告。
显示视图控制器的代码:
func displayController(controller:UIViewController) {
self.addChildViewController(controller)
controller.view.setTranslatesAutoresizingMaskIntoConstraints(false)
controller.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height)
self.view.addSubview(controller.view)
self.view.addConstraint(NSLayoutConstraint(item: controller.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: controller.view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: controller.view, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: controller.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
controller.didMoveToParentViewController(self)
self.currentViewController = controller;
}
移除视图控制器的代码
func hideController(controller:UIViewController) {
controller.willMoveToParentViewController(nil)
controller.view.removeFromSuperview()
controller.removeFromParentViewController()
if self.currentViewController == controller {
self.currentViewController = nil
}
}
交换视图的代码只调用了这两个方法:
func switchToViewController(controller:UIViewController) {
if self.currentViewController != nil {
self.hideController(self.currentViewController!)
}
self.displayController(controller)
}
两个子视图控制器使用相同的 .xib 和一个在 InterfaceBuilder 中设置了约束的大按钮。
第一次添加和删除这些子视图时,它们显示正常,没有警告。
再次添加第一个视图后,按钮的高度错误,我收到“无法同时满足约束”警告。
2015-02-23 21:40:17.223 Swift Container View Controller[27976:832141] Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x7fa57a41eed0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fa57a715b40(667)]>",
"<NSLayoutConstraint:0x7fa57a71d1c0 V:[UIButton:0x7fa57a71bce0'Switch to Yellow View']-(413)-| (Names: '|':UIView:0x7fa57a71cff0 )>",
"<NSLayoutConstraint:0x7fa57a71d260 V:|-(262)-[UIButton:0x7fa57a71bce0'Switch to Yellow View'] (Names: '|':UIView:0x7fa57a71cff0 )>",
"<NSLayoutConstraint:0x7fa57a71d300 V:[UIButton:0x7fa57a71bce0'Switch to Yellow View'(125)]>",
"<NSLayoutConstraint:0x7fa57a48ff10 UIView:0x7fa57a71cff0.height == UIView:0x7fa57a715b40.height>"
)
我相当肯定按钮上的约束是正确的,因为它们第一次布局正确,但随后会中断。
【问题讨论】:
-
我打赌你的 xib 是 800 点高? iPhone 6 的高度为 667 点。错误是说它不能使 800 = 667,但为什么它最初没有失败我不能说。
标签: ios xcode swift uikit autolayout