2016-05-30 11 views
1

私にはUICollectionViewがあります。プログラムで定義された制約スウィフトiOS

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は今だけでなく、画面の下部に触れていると、2つのビューが重複していることです。私は、次の制約でこれを解決しようとした:

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. 
 
\t Probably at least one of the constraints in the following list is one you don't want. 
 
\t Try this: 
 
\t \t (1) look at each constraint and try to figure out which you don't expect; 
 
\t \t (2) find the code that added the unwanted constraint or constraints and fix it. 
 
\t (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.

+0

iOS9以降をサポートしている場合は、NSLayoutAnchorクラスを調べることをお勧めします。これにより、プログラムで制約を書きやすくなります。 – Soberman

+0

NSLayoutConstraintと文字列形式を使用して直接制約を書き込むことはお勧めできません。面白いインターフェースを備えたhttps://github.com/robb/Cartographyを使用してみてください。 – dydus0x14

答えて

2

自動レイアウトのための基本的なtutorialsをチェックアウトしてください。それは単にあなたにIBと拘束の必要な背景を与えるだけです。

一般的に、デバッグログは必要なものすべてです。問題の正確な情報が表示されます。さんが詳しく

NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0] 

UICollectionView:0x7fece08c0a00でそれについて考えてみよう - それはあなたのコレクションビューだ、それは UIView:0x7fece04992d0簡単です - それはあなたのmessageInputContainerViewだ - それはだ次の制約からクリア:

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

のコード行によって生成されます:だから

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

、あなたが見ることができれば - uicollectionビューがNSAutoresizingMaskLayoutCを持っていますそれはデフォルトで生成されたものです。あなたは2つの方法を持っています - 先導、訓練、トップ(必要な値まで)、ボトム拘束を値48で設定してコレクションビューの制約を設定します。

2番目の方法 - プログラムによって自動生成された制約を削除し、次のようになります。

[collectionView removeConstraints: [collectionView constraints]]; 
     // top constraint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)) 
    // leading constraint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0)) 
    // trailing constaint 
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0)) 
    // your constraint 
    let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0) 

    view.addConstraint(newConstraint) 

これが役に立ちます。

関連する問題