2016-04-08 6 views
10

ビューコントローラーの右側に添付された一種の「パネル」として機能するビューを作成しようとしています。ビューコントローラーの余白にバインドされた制約をプログラムで作成する

、それはしかし、私はちょうどそれが右のように見えることはできません300

の静的な幅で、親ビューコントローラの末尾、上、下の余白にバインドされていることを、私は」制約を破るか何かをすると、xcodeは私に違法だと伝えます。

私は間違っていますか?ここで

コードがコントローラに

let myView = UIView() 
    view.backgroundColor = UIColor.redColor() 
    self.view.addSubview(view) 
    let topConstraint = NSLayoutConstraint(item: myView, 
              attribute: .Top, 
              relatedBy: .Equal, 
              toItem: self.topLayoutGuide, 
              attribute: .Bottom, 
              multiplier: 1, 
              constant: 0) 

    let trailingConstraint = NSLayoutConstraint(item: self.view, 
               attribute: .TrailingMargin, 
               relatedBy: .Equal, 
               toItem: myView, 
               attribute: .Trailing, 
               multiplier: 1, 
               constant: 0) 

    let bottomConstraint = NSLayoutConstraint(item: self.bottomLayoutGuide, 
               attribute: .Top, 
               relatedBy: .Equal, 
               toItem: myView, 
               attribute: .Bottom, 
               multiplier: 1, 
               constant: 0) 

    let widthConstraint = NSLayoutConstraint(item: myView, 
              attribute: .Width, 
              relatedBy: .Equal, 
              toItem: nil, 
              attribute: .NotAnAttribute, 
              multiplier: 1, 
              constant: 300) 

    self.view.addConstraints([trailingConstraint]) 
    view.addConstraints([topConstraint, bottomConstraint, widthConstraint]) 
+1

なぜVisual Format Languageを使用しないのですか? – Eendje

答えて

38

は実際にあなたのコードに問題あなたはDということですid translatesAutoresizingMaskIntoConstraintsmyviewfalseに設定されていない場合、自動レイアウトの制約を使用する場合は、ビューのtranslatesAutoresizingMaskIntoConstraintsをfalseに設定する必要があります。 別の問題は、myviewをself.viewに追加していないということです。私はコードを更新しています。

以下のコードをViewControllerに挿入します。

let myView = UIView() 
myView.backgroundColor = UIColor.redColor() 
self.view.addSubview(myView) 
myView.translatesAutoresizingMaskIntoConstraints = false 

view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1, constant: 0)) 
view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Bottom, relatedBy: .Equal, toItem: self.bottomLayoutGuide, attribute:.Top, multiplier: 1, constant: 20)) 

view.addConstraint(NSLayoutConstraint(item: myView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute,multiplier: 1, constant: 300)) 
view.addConstraint(NSLayoutConstraint(item: myView, attribute: .TrailingMargin, relatedBy: .Equal, toItem: view, attribute: .TrailingMargin, multiplier: 1, constant: 0)) 
+1

より簡単にプログラムで制約を追加したい場合は、httpsを使用してください(例:H:[panel(300)] | 'V:| [toplayout] [panel] [botlayout] ://github.com/keshavvishwkarma/KVConstraintExtensionsMaster。 –

+0

あなたは本当に私の答えが好きなら、あなたも投票します。 –

0

であるあなたがMYVIEWとしてのUIViewを作成するが、それ自体を表示することもself.viewするビューも制約を追加している、あなたのコード内のいくつかのあいまいさがあるようです。コードを修正し、myViewでビューを置き換えてください。 次に、setTranslayesAutoresizingMaskIntoConstraintsをfalseに設定します。 次に、すべての制約をself.viewに追加します。これはあなたの問題を解決するはずです。

myView.setTranslatesAutoresizingMaskIntoConstraints(false) 
self.view.addConstraints([trailingConstraint, bottomConstraint, widthConstraint]) 

VFLもより良いクリーンなアプローチです。これは実際に制約の設定方法を視覚化します。

5

上記のコード例では、いくつかの場所でviewmyViewが混在しているようです。いずれにしてもwidthConstraintmyViewおよびtopConstraint,trailingConstraintに、bottomConstraintself.viewに追加する必要があります。この理由は、制約に関連する両方のビューをレイアウトする最も近いスーパービュー祖先に制約を追加する必要があるためです。子ビュー属性を親ビューの属性に制約する場合、親ビューに制約を追加する必要があります。親ビューと子ビューの両方をレイアウトするためです。 2つの兄弟ビューの間に制約がある場合、両方のビューを配置する最も近い祖先であるため、親ビューに制約が追加されます。

iOS 9.0以上をターゲットにできる場合は、これらの種類の制約を作成するために、新しいNSLayoutAnchor APIとNSLayoutDimension APIを使用する方がはるかに簡単で簡単です。また、厳密な型チェックを提供し、コンパイラは正しさを検証できます。これらの新しいAPIを使用すると、あなたのコード例では、単純になっていないでしょう:

let myView = UIView() 
myView.backgroundColor = UIColor.redColor() 
self.view.addSubview(myView) 

let margins = self.view.layoutMarginsGuide 
myView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true 
myView.topAnchor.constraintEqualToAnchor(margins.topAnchor).active = true 
myView.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor).active = true 
myView.widthAnchor.constraintEqualToConstant(300.0).active = true 

明示的に右のビューに制約を追加する必要はありませ、などあなたがここに制約を作成するこの方法についての詳細を読むことができます:

https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutAnchor_ClassReference/

、ここで:

https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutDimension_ClassReference/

関連する問題