Chapter 16 Auto layout: Programmatic Constraints

1. The line of code regarding translating constraints has to do with an older system for scaling interfaces - autoresizing masks. Before Auto Layout was introduced, iOS applications used autoresizing masks to allow views to scale for different-sized screens at runtime.

-(void)viewDidLoad

{

[super viewDidLoad];

UIImageView *iv = [[UIImageView alloc] initWithImage:nil];

// The contentMode of the image view in the XIB was Aspect Fit;

iv.contentMode = UIViewContentModeScaleAspectFit;

// Do not produce a translated constraint for this view

iv.translatesAutoresizingMaskIntoConstraints = NO;

// The image view is a subview of the view

[self.view addSubview:iv];

// The image view was pointed to by the imageView property

self.imageView = iv;

}

2. Every view has an autoresizing mask. By default, iOS creates constraints that match the autoresizing mask and adds them to the view. These translated constraints will often conflict with explicit constraints in the layout and cause an unsatisfiable constraints problem. The fix is to turn off this default translation by setting the property translatesAutoresizingMaskIntoConstraints to NO.

3. Apple recommends using a special syntax called Visual Format Language (VFL) to create constraints programmatically. VFL is a way of describing constraints in a literal string. You can describe multiple  constraints in one visual format string. A single visual format string, however, cannot describe both vertical and horizontal constraints.

Here is the horizontal spacing constraints for the image view as a visual format string:

@“H : | -0- [imageView] -0-|”

The H: specifies that these constraints refer to horizontal spacing. The view is identified inside square brackets. The pipe character (|) stands for the view’s container. This image view, then, will be 0 points away from its container on its left and right edges.

When the number of points between the view and its container (or some other view) is 0, the dashes and the 0 can be left out of the string:

@“H: | [imageView] | “

The string for the vertical constraints looks like this:

@“V : [dateLabel] -8- [imageView] -8- [toolbar]”

Notice that “top” and “bottom” are mapped to “left” and “right”, respectively, in this necessarily horizontal display of vertical spacing. The image view is 8 points from the date label at its top edge and 8 points from the toolbar at its bottom edge.

The dash by itself sets the spacing to the standard number of points between views, which is 8. You could write this same string like this:

@“V : [dateLabel] - [imageView] - [toolbar]”

4. A constraint is an instance of the class NSLayoutConstraint. When creating constraints programmatically, you explicitly create one or more instances of NSLayoutConstraint and then add them to the appropriate view object. Creating and adding constraints is one step when working with a XIB, but it is always two distinct steps in code. You create constraints from a visual format string using the NSLayoutConstraint method:

+(NSArray *)constraintsWithVisualFormat:(NSString*)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary*)metrics views:(NSDictionary*)views

This method returns an array of NSLayoutConstraint objects because a visual format string typically creates more than one constraint.

5. Add instances of NSLayoutConstraint to the view using the UIView method

-(void)addConstraints:(NSArray*)constraints

Usually, the closest common ancestor of the views that are affected by the constraint. Here is a list of rules you can follow to determine which view you should add constraints to:

If a constraint affects two views that have the same superview (such as the constraint labeled “A” in Figure 16.1), then the constraint should be added to their superview.

If a constraint affects just one view (the constraint labeled “B”), then the constraint should be added to the view being affected.

If a constraint affects two views that do not have the same superview but do share a common ancestor much higher up on the view hierarchy (the constraint labeled “C”), then the first common ancestor gets the constraint.

If a constraint affects a view and its superview (the constraint labeled “D”), then this constraint will be added to the superview.

6. Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label’s intrinsic content size is the size of the image that you selected.

Auto layout takes this information into consideration by creating intrinsic content size constraints for each view. Unlike other constraints, these constraints have two priorities: a content hugging priority and a content compression resistance priority.

Content hugging priority: tells Auto Layout how important it is that the view’s size stay close to, or “hug”, its intrinsic content. A value of 1000 means that the view should never be allowed to grow larger than its intrinsic content size. If the value is less than 1000, then Auto Layout may increase the view’s size when necessary.

Content compression resistance priority: tells Auto Layout how important it is that the view avoid shrinking, or “resist compressing”, its intrinsic content. A value of 1000 means that the view should never be allowed to be smaller than the view should never be allowed to be smaller than its intrinsic content size. If the value is less than 1000, then Auto Layout may shrink the view when necessary.

时间: 2024-12-16 22:54:29

Chapter 16 Auto layout: Programmatic Constraints的相关文章

iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

iOS Programming? Auto Layout: Programmatic Constraints? 1.? However, if your views are created in code, then you will need to constrain them programmatically. 如果你的view是由代码创建的,那么你需要用编程限制它了. To have a view to work with, you are going to recreate the im

Auto Layout: Programmatic Constraints - BNR

继续Auto Layout - BNR篇. 打开BNRDetailViewController.m文件,重载viewDidLoad方法来创建UIImageView对象.当你想要给通过加载NIB文件创建的视图层级添加约束时,需要重载viewDidLoad方法.如下: 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 UIImageView *iv = [[UIImageView alloc] initWithImage:nil]; 5 iv.co

Auto Layout Guide----(二)-----Auto Layout Without Constraints

Auto Layout Without Constraints 没有约束的自动布局 Stack views provide an easy way to leverage the power of Auto Layout without introducing the complexity of constraints. A single stack view defines a row or column of user interface elements. The stack view a

Auto Layout Guide----(一)-----Understanding Auto Layout

Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. For example, you can constrain a button so that it is horizontally centered

Auto Layout Guide----(三)-----Anatomy of a Constraint

Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equations. Each constraint represents a single equation. Your goal is to declare a series of equations that has one and only one possible solution. A samp

Chapter 15 Introduction to Auto Layout

1. You do not define a view’s alignment rectangle directly. You do not have enough information (screen size!)to do that. Instead, you provide a set of constraints. Taken together, these constraints allow the system to determine the layout attributes,

iOS 使用LayoutGuide 来限制控件的位置,配合Auto Layout constraints

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [self.view addSubview:button]; [button setTranslatesAutoresizingMaskIntoConstraints: NO]; // 得到当前视图的最低基准限制,这个是对于Auto Layout constraint来说的. id bottomGuide = self.bottomLayoutGu

转////iOS 8 Auto Layout界面自动布局系列1-自动布局的基本原理

iOS 8 Auto Layout界面自动布局系列1-自动布局的基本原理 http://blog.csdn.net/pucker/article/details/41832939 标签: iosinterface苹果布局界面 2014-12-10 00:25 11286人阅读 评论(2) 收藏 举报  分类: iOS开发(19)  版权声明:本文为博主原创文章,未经博主允许不得转载. 苹果今年如约放出了新的iPhone 6与iOS 8系统,SDK针对新的设备和系统的界面适配也进行了若干改进,因此

使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手投稿 6 23450 Auto Layout自动布局VFL 招聘信息: 高级iOS开发工程师 高级PHP开发工程师 iOS高级研发工程师 iOS开发工程师 高级iOS手机应用软件开发工程师(培训讲师) 高级Cocos2d-x游戏开发工程师(培训讲师) iOS手机软件开发工程师 iOS工程师 Web后