Auto Layout三种形态

Auto Layout builds constraints in three ways. So far, you’ve read about two of them. First, you can lay out your constraints in Interface Builder(IB) and customize them to your needs.Second, you can build single constraints in code. The NSLayoutConstraint class offers the constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: method, which enables you to create constraints one at a time, relating one item’s attribute to another. In this chapter, you’ll read about the third way: using a visual formatting language to express how items are laid out along vertical and horizontal axes.

This chapter explores what these visual constraints look like, how you build them, and how you use them in your projects. You’ll read about how metrics dictionaries and constraint options extend visual formats for more flexibility. And you’ll see numerous examples that demonstrate these formats and explore the results they create.

There’s one thing to keep in mind throughout: All constraints are members of the NSLayoutConstraint class, regardless of how you build them. Every constraint stores a “y relation mx +b” rule within an Objective-C object and expresses that rule through the Auto Layout engine. Visual formats are another tool that takes you to that same place.

使用IB,代码写的NSLayoutConstraint,还有Visual formats转化的NSLayoutConstraint.


Layout constraints (NSLayoutConstraint class, public)

These rules specify view geometry. They restrict a view’s position and size by relating a view to other views and / or to constant values.

Content size constraints(NSContentSizeLayoutConstraint class, private)

Content size rules specify how a view’s size should relate to its content. For example, content hugging rules avoid adding padding, and content compression rules prevent clipping.

Autosizing constraints (NSAutoresizingMaskLayoutConstraint class, private)

Autosizing constraints translate the older autosizing masks into the Auto Layout system.

Layout support constraints (_UILayoutSupportConstraint class, private)

Introduced in iOS7, layout support constraints establish practical boundaries for the tops and bottoms of your view controller instances. These constraints restrict content from overlapping with obstacles such as status bars.


Prototyping constraints (NSIBPrototypingLayoutConstraint class, private)

Also new to iOS 7, prototyping constraints are constraints that Interface Builder(IB) adds for you. They enable you to build interfaces incrementally yet still have a working interface to test. When you ship apps, your code should not use, reference, or otherwise include prototyping constraints.


In IB, you create layout support constraints by constraining views to top or bottom layout guide proxies.

UIView *topLayoutGuide = (UIView *)self.topLayoutGuide;
CONSTRAIN(@”V:[topLayoutGuide][textView]|”, topLayoutGuie, textView);

In this example, a text view stretches between the top guide, which defines the top of the application context space, and the bottom of the parent.

猜你喜欢

转载自lizhuang.iteye.com/blog/2018697
今日推荐