2017-03-25 8 views
1

NSLayoutConstraintを使用して自動レイアウトを生成する方法がわかりません。オートレイアウトをプログラムで(NSLayoutConstraint):内側のビューを外側に表示し、min(幅、高さ)に制限します。

外側のビューの幅と高さの小さい方を使用して、内側のビューを外側のビューの中央に配置したいと考えています。最後に、倍率を適用したいと思います。

Auto Layout Centered

ここに私のコードです。

 NSLayoutConstraint.activate([ 
      inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor), 
      inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor), 
      inner.widthAnchor.constraint(equalTo: outer.heightAnchor, multiplier: imageScale), 
      inner.widthAnchor.constraint(equalTo: outer.widthAnchor, multiplier: imageScale), 
      inner.heightAnchor.constraint(equalTo: inner.widthAnchor) 
    ]) 

これがために働く:

  • 幅==高さ
  • 幅<高さ

しかし、私はこれを取得幅は>高さ:

enter image description here

私は何が欠けていますか?これは、フレームを使用して行うことは非常に簡単です:

let innerWidth = min(outer.frame.width, outer.frame.height) * imageScale 
let innerHeight = innerWidth 
inner.frame = CGRect(x: (outer.frame.width -innerWidth)/2, 
        y: (outer.frame.height - innerHeight)/2, 
        width: innerWidth, height: innerHeight) 

答えて

2

まず、それはあなたがあなたのコード内で二回inner.widthAnchor制約を設定していることを少し奇妙です。一度だけ設定してください。

また、実際の外側のビューのフレームに基づいて、内側のビューの外側のアンカーを選択する必要があります。最小の外寸が幅の場合は、内側ビューの幅をouter.widthAnchor * imageScaleに制限します。それ以外の場合は、最小の外寸が高さである場合は、outer.heightAnchor * imageScaleに制限します。

私はあなたが探しているレイアウトを取得するためにこれが(私は単純な単一のビューのプロジェクトを作成した)作品:

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let inner = UIView() 
     inner.backgroundColor = .yellow 
     inner.translatesAutoresizingMaskIntoConstraints = false 

     let outer = UIView() 
     outer.backgroundColor = .blue 
     outer.translatesAutoresizingMaskIntoConstraints = false 

     view.addSubview(outer) 
     view.addSubview(inner) 

     // get these from somewhere, e.g. outerWidth.frame.size 
     let outerWidth = CGFloat(200) 
     let outerHeight = CGFloat(400) 
     let outerAnchor: NSLayoutDimension 

     if outerWidth >= outerHeight { 
      outerAnchor = outer.heightAnchor 
     } else { 
      outerAnchor = outer.widthAnchor 
     } 

     let imageScale = CGFloat(0.5) 

     NSLayoutConstraint.activate([ 
      outer.widthAnchor.constraint(equalToConstant: outerWidth), 
      outer.heightAnchor.constraint(equalToConstant: outerHeight), 
      outer.centerXAnchor.constraint(equalTo: view.centerXAnchor), 
      outer.centerYAnchor.constraint(equalTo: view.centerYAnchor), 

      inner.centerXAnchor.constraint(equalTo: outer.centerXAnchor), 
      inner.centerYAnchor.constraint(equalTo: outer.centerYAnchor), 
      inner.widthAnchor.constraint(equalTo: outerAnchor, multiplier: imageScale), 
      inner.heightAnchor.constraint(equalTo: inner.widthAnchor) 
     ]) 
    } 
} 
+0

ありがとうございました。はい、これは現在動作しています。私はどこかから次元を取得しなければならないのは残念です。このためにフレームを使用したいだけです。 – MH175

+1

フレームを使用するよりもはるかに柔軟性があるので、制約を明確に使用してください(そして、SnapKitを見てください)。しかし、(あなたが見てきたように)、レイアウトが発生した後も情報を得るためにフレームを時々見なければなりません。 – par

+0

このようなものが適応できるかどうか疑問に思う:http://simblestudios.com/blog/development/percentage-width-in-autolayout.html – MH175

関連する問題