以编程方式定义的约束Swift iOS

时间:2016-05-30 18:56:59

标签: ios swift storyboard uicollectionview constraints

我有UICollectionView。我还有以下视图,我在代码中生成:

let messageInputContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.whiteColor()
    return view
}()

现在,我想添加messageInputContainerView以便它附加到屏幕底部,而UICollectionView就在它的正上方。目前我有以下限制:

view.addSubview(messageInputContainerView)
    view.addConstraintsWithFormat("H:|[v0]|", views: messageInputContainerView)
    view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView)

    bottomConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)
    view.addConstraint(bottomConstraint!)

问题是collectionView现在也触摸屏幕的底部,两个视图重叠。我尝试使用以下约束来解决这个问题:

let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0)

view.addConstraint(newConstraint)

然而,这只是一堆错误:



016-05-30 19:50:26.153 City[1858:79868] Unable to simultaneously satisfy constraints.
	Probably at least one of the constraints in the following list is one you don't want. 
	Try this: 
		(1) look at each constraint and try to figure out which you don't expect; 
		(2) find the code that added the unwanted constraint or constraints and fix it. 
	(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x7fece04d10e0 h=-&- v=-&- UICollectionView:0x7fece08c0a00.midY == UICollectionViewControllerWrapperView:0x7fece04125f0.midY>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fece04d1150 h=-&- v=-&- UICollectionView:0x7fece08c0a00.height == UICollectionViewControllerWrapperView:0x7fece04125f0.height>",
    "<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>",
    "<NSLayoutConstraint:0x7fece04d7620 UIView:0x7fece04992d0.bottom == UICollectionViewControllerWrapperView:0x7fece04125f0.bottom>",
    "<NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

请查看基本tutorials的autolayout。它将简单地为您提供IB和约束所需的背景。

通常只需要调试日志 - 它会告诉您问题的确切信息。让我们详细考虑一下

pip install requests 

NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0] - 这是你的集合视图,很简单 UICollectionView:0x7fece08c0a00 - 这是你的messageInputContainerView - 从以下约束中可以清楚地看出:

UIView:0x7fece04992d0

由这行代码生成:

"<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>"

所以,如果你能看到 - uicollection视图有NSAutoresizingMaskLayoutConstraint - 那么,那些是默认生成的。您有两种方法 - 通过IB设置集合视图约束 - 设置前导,训练,顶部(到您需要的任何值)和底部约束值48。

第二个 - 以编程方式删除自动生成的约束并添加新约束,如下所示:

view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView)

希望这会有所帮助。