Auto Layout三种形态

本文介绍了AutoLayout的三种约束构建方式:通过Interface Builder、代码编写NSLayoutConstraint以及使用视觉格式语言。文章详细探讨了视觉格式语言如何表达布局,并通过实例展示了其灵活性与应用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值