私の設計では、ボタンが他の 2 つのオブジェクトの中央に配置される必要があります。オブジェクトの 1 つがビューの垂直方向の中央にあります (緑)。もう一方のオブジェクトは、下端 (緑) から少し離れています。ここでのタスクは、3 番目のオブジェクト (赤) を他の 2 つのオブジェクトの中央に配置することです。xcode6 の新しい制約を使用しており、ビューは wRegular hRegular モードです。これはコードで簡単ですが、ストーリーボードを使用してこれを達成しようとしています。
2 に答える
There are a number of approaches:
In iOS 9, the easiest would be to define a vertical
UIStackedView
with adistribution
of "equal spacing" and thenaddArrangedSubview
the three circular subviewsAnother option in iOS 9 would be to create two
UILayoutGuide
(which would represent the two question marks in your image), add them to the shared superview withaddLayoutGuide
and define them to be the same size as each other. The corresponding VFL might look like:"V:|[greenView1(==100)]-[layoutGuide1]-[redView(==50)]-[layoutGuide2(==layoutGuide1)]-[greenView2(==greenView1)]|"
In earlier iOS versions, rather than using
UILayoutGuide
, you could just create two "space" views (UIView
with clear background so they're not visible) then define a constraint such that their heights are identical, and then define vertical spacing constraints between the five views (the three circles and the two spacer views) with a constant of zero.It might look like:
I've made those "spacer" views visible, to illustrate the idea, but obviously you'd set them to be transparent so you can't see them in the UI.
This is logically equivalent to the
UILayoutGuide
approach in iOS 9, except that theUIView
"spacer" views just carry a little more overhead than aUILayoutGuide
. But in iOS versions before 9, this is the common approach to this problem.