2016-08-08 12 views
1

UIView(またはUIStackView)内に複数の要素(ラベルとボタン)を配置したいと考えています。私はUIStackViewにCenter AlignmentとDistribution FillとSpacing 10の要素を追加しようとしましたが、私の要素は左から位置を取得し続けます。私はセンターを変えようとした。まだ運がありません。 - 2つの要素で複数の要素をUIViewの中央に配置する方法

[ボタン------- -------ラベル]:私が達成しようとしている何

[--label-button-button--] 3要素で。

スペーサを使用せずにこれを達成する方法はありますか?理論的には私は制約を使うことができるかもしれませんが、要素の数が変わったために良いアイデアだとは思いません。

let simpleView = UIView() 
simpleView.addSubview(text) 
simpleView.addSubview(button) 


let leftLeading = NSLayoutConstraint(item: text, attribute: 
    .LeadingMargin, relatedBy: .Equal, toItem: simpleView, 
        attribute: .LeadingMargin, multiplier: 1.0, 
        constant: 10) 

let leftLeading2 = NSLayoutConstraint(item: button, attribute: 
    .LeadingMargin, relatedBy: .Equal, toItem: simpleView, 
        attribute: .LeadingMargin, multiplier: 1.0, 
        constant: 10) 

simpleView.addConstraint(leftLeading) 
simpleView.addConstraint(leftLeading2) 


simpleView.translatesAutoresizingMaskIntoConstraints = false 
simpleView.sizeToFit() 

newView.addSubview(simpleView) 

let horizontalConstraint = NSLayoutConstraint(item: simpleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: newView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) 
newView.addConstraint(horizontalConstraint) 

let verticalConstraint = NSLayoutConstraint(item: simpleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: newView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0) 
newView.addConstraint(verticalConstraint) 
これは私が得るものですので、私は何か間違ったことしなければならない

は(newViewは、ボタンが青色で、テキストラベルが欠落している赤です):

enter image description here

+0

均等に配布しようとしましたか? – KotaBear233

答えて

0

は、ビューを作成し、センタービューあなたが必要とする場所。そのビューの中で、各要素を挿入し、それぞれに要素の間に必要な5桁または任意の間隔を持つプログラムNSLayoutConstraintを与えます。

VIEW [ELEMENT ELEMENT ELEMENT]

数は問題ではありません。 NSLayoutConstraintsを調べて、view.addConstraintsを使用していることを確認してください。

+0

私の編集を見てください。 – HelloimDarius

関連する問題