我有一個非常簡單的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)
}
這兩個子視圖控制器都使用與在InterfaceBuilder中設置約束的大按鈕相同的.xib。
第一次添加和刪除這些子視圖時,它們顯示正常,沒有警告。
一旦第一種觀點再次添加按鈕的高度是錯誤的,我得到一個「無法同時滿足約束」的警告。
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,但爲什麼它不會失敗,最初我不能說。 – 2015-02-24 04:50:43