Auto Layout in iOS

I spend a day to learn how to make auto layout in iOS. I begin with an article How to use UIViews with auto layout programmatically. It teaches me add views from the first then add size constraints and position contraints later. They are using NSLayoutConstraint.

After that, I find a thread at StackOverflow. They show off 3 ways to make Auto Layout programmatically

  • Pure Classic style, which use NSLayoutConstraint with direct params
  • Pure Visual Format Language style, which use NSLayoutConstraint.constraintsWithVisualFormat() and Visually Format Languague. You can find more information about it at Apple Library
  • Mixed style (Classic + Visual Format Language)

I also try to apply to Career 365 splashscreen but It doesn’t seem to be working. Struggling about an hour, finally I decide to find some more help from community and I find out Cartography. It’s such a good library which has about 1k5 stars from Github Users. Their syntax are good too. I think I should give it a try tmr.

1
2
3
4
5
6
7
8
9
10
11
layout(view1, view2) { view1, view2 in
    view1.width   == (view1.superview!.width - 50) * 0.5
    view2.width   == view1.width - 50
    view1.height  == 40
    view2.height  == view1.height
    view1.centerX == view1.superview!.centerX
    view2.centerX == view1.centerX

    view1.top >= view1.superview!.top + 20
    view2.top == view1.bottom + 20
}

Comments