2016-10-05 4 views
3

多くのソリューションと適用された自動レイアウト制約がありますが、依然として解決策が得られません。同じ幅をボタンに適用すると、自動レイアウトが正しく設定されない

現在の結果:すべてのボタンに与えられた

Result after Auto layout set

制約:

Constrains given to buttons

ビュー階層:

enter image description here

+0

希望するソリューションは何ですか? –

+0

あなたのストーリーボードコントローラに警告があるかどうか教えていただけますか? –

+0

@Damien:それは何の警告も出されていません – user2017279

答えて

0

(すなわち、コードなしで:いないプログラムで)、これは解決策は次のとおりです。

ビューに追加する必要はありません。だけで次の手順を実行します(ボタンは任意の制約を-yet-持っていないことを、単にビューの下にそれらを追加

enter image description here

注:

はこれで起動してみましょうまたはあなたがそれらを表示したい場合は、このソリューションのために、私はそれらを画面の下部に表示します)。それらの3つのサイズが同じであることを確認してください。 オレンジボタンに

追加の制約:

は、以下の制約を追加: リーディング、底空間と同じ高さ。

enter image description here

青色ボタンに制約を追加する:

次の制約を追加: 末尾、下部空間と同じ高さ。垂直水平間隔および中心:以下の制約条件を追加して、オレンジ色のボタンにシアンボタンから

  • CTRL +ドラッグ:シアンボタンに制約を追加

    enter image description here

    水平間隔:

enter image description here

  • CTRL +ドラッグシアンボタンからの青色ボタンに、次の制約を追加。

enter image description here

  • 底空間と同じ高さ。

enter image description here

は、これまでのところは良い、我々はほぼ完了しています!

ここで、3つのボタンを選択して、次の制約:と等しい幅を追加します。

enter image description here

あなたのボタンは次のようになります。今

enter image description here

、あなたがしなければならないすべては、2つの水平方向の間隔制約のそれぞれを選択して、0にその定数を設定していますサイズ・インスペクタから:

enter image description here

enter image description here

、そこにあなたが行く:

enter image description here

私は、これは助け乾杯アップを願っています。

0

.Swift 3.0 ..コード内の溶液のため申し訳ありません:

let button1 = UIButton() 
    let button2 = UIButton() 
    let button3 = UIButton() 
    let buttons = [button1, button2, button3] 
    button1.backgroundColor = .red 
    button2.backgroundColor = .blue 
    button3.backgroundColor = .green 
    buttons.forEach { 
     $0.translatesAutoresizingMaskIntoConstraints = false 
     view.addSubview($0) 
    } 
    buttons.forEach { 
     let cnt1 = NSLayoutConstraint(item: $0, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0) 
     let cnt2 = NSLayoutConstraint(item: $0, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 0.2, constant: 0) 
     view.addConstraints([cnt1, cnt2]) 
    } 
    let cnt1 = NSLayoutConstraint(item: button1, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0) 
    let cnt2 = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: button3, attribute: .trailing, multiplier: 1, constant: 0) 
    let cnt3 = NSLayoutConstraint(item: button2, attribute: .width, relatedBy: .equal, toItem: button1, attribute: .width, multiplier: 1, constant: 0) 
    let cnt4 = NSLayoutConstraint(item: button3, attribute: .width, relatedBy: .equal, toItem: button2, attribute: .width, multiplier: 1, constant: 0) 
    let cnt5 = NSLayoutConstraint(item: button2, attribute: .leading, relatedBy: .equal, toItem: button1, attribute: .trailing, multiplier: 1, constant: 0) 
    let cnt6 = NSLayoutConstraint(item: button3, attribute: .leading, relatedBy: .equal, toItem: button2, attribute: .trailing, multiplier: 1, constant: 0) 
    view.addConstraints([cnt1, cnt2, cnt3, cnt4, cnt5, cnt6]) 
0

はあなたが同じ高さと幅を持って水平に三つのボタンをレイアウトしようとしていますか?私はあなたがUIViewを使用せずにこれを達成できると思います。私は次のような制約を設定します。

enter image description here

これは、シミュレータのスクリーンショットです。あなたはさせて頂いておりたいものを3つのボタンがお互いに等しいと仮定すると、Interface Builderのを使用して、画面の幅を埋める

Simulator screenshot

関連する問題