0

I have a layout like picture. This is design in 4s, when I run in 7 or 7s+, I want 3 white blocks is center as 4s and increase size to match with new screen. Any help? I use Xcode 8.1. Thanks

4s

iPhone 4s


7 or 7s+ (Fail)

enter image description here


7 or 7s+ (I hope this)

7 or 7s+ (I hope this)

ZNApps
  • 55
  • 2
  • 8

1 Answers1

1

Place your 3 blocks into a horizontal stack view (UIStackView) with the desired spacing, and then center the stack view into your content view.

EDIT:

If you need to support iOS versions older than iOS 9 then you can't use stack views. In that case your job is a lot more complicated.

What I've done is to place the views I want to center inside another view, which I center horizontally on the screen. I'll call that the container view.

I'll call the views you want to space evenly "white views."

I add constraints to set my white views to a fixed width, and insert "spacer views" between each white view. I create a constraint on each spacer view that sets its width equal to the widths of all the other spacer views, and lock the leading and trailing space of each view to it's nearest neighbors. (were the outermost spacer views are locked to their superviews.

If you set your spacer views to a fixed width, and make your container view's height match the height of a white view, tie it's leading edge to the leading edge of the left-most spacer view, and tie it's trailing edge to the trailing edge of the right-most spacer view, then the container view sizes automatically to contain all the white views and spacer views, and stays centered, so the views within it stay centered.

EDIT #2:

See this thread, that illustrates both the spacer approach and another technique that uses proportional centering constraints:

Evenly space multiple views within a container view

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • That I need, so, you can help me this steps? I try and I want increase size white blocks with ratio 1:2, same with 4s. I try put to stackview, and center, but in 7/7s+, white blocks don't keep ratio, its very height – ZNApps Dec 01 '16 at 14:29
  • I want run this from iOS7, without stack view – ZNApps Dec 01 '16 at 14:50
  • 1
    See the edit to my answer. Why are you worrying about an iOS version that's 3 major versions out of date? The number of users running iOS 8 and earlier is likely less than 10% of total users, and most of those are running old hardware that can't support the new OS versions. The stats I've seen say that those using legacy hardware don't buy apps, and thus don't really affect your sales. I'd suggest ignoring OS versions older than 1 major release unless you have a specific reason to do otherwise. – Duncan C Dec 01 '16 at 15:55
  • Yep, I see, thanks :) this is required by my company, not me. – ZNApps Dec 03 '16 at 08:22