AutoLayout与Size Class相关

关于Size Class相关的文章:

  • 初探 iOS8 中的 Size Class
  • iOS8 AutoLayout与Size Class 自悟
  • Size Classes Design Help
  • What’s New in iOS
  • Size Classes with Xcode 6
  • Size Classes with Xcode 6:为所有的尺寸准备一个Storyboard
  • Xcode6中自动布局autolayout和sizeclass的使用

关于AutoLayout相关文章

  • 为iPhone6设计自适应布局(一 )
  • ADAPTIVE LAYOUTS FOR iPHONE 6
  • Auto Layout 进阶
  • http://blog.csdn.net/think_ma/article/details/42044615
  • Tutorial: Auto Layout Part 1

1.Intrinsic Content Size 和 Content Hugging & Compression Resistance Priorities

The resize behavior of a view with an intrinsic content size can be controlled by specifying compression resistance and content hugging priorities. A view with a high compression resistance priority and a low content hugging priority will be allowed to grow but will resist shrinking in the corresponding dimension. Similarly, a high compression resistance priority in conjunction with a high content hugging priority will cause the view to resist any form of resizing, keeping the view as close as possible to its intrinsic content size.

Intrinsic Content Size

The intrinsic content size is the size a view prefers to have for a specific content it displays. For example, UILabel has a preferred height based on the font, and a preferred width based on the font and the text it displays. A UIProgressView only has a preferred height based on its artwork, but no preferred width. A plain UIView has neither a preferred width nor a preferred height.

Compression Resistance and Content Hugging

Each view has content compression resistance priorities and content hugging priorities assigned for both dimensions. These properties only take effect for views which define an intrinsic content size, otherwise there is no content size defined that could resist compression or be hugged.

Behind the scenes, the intrinsic content size and these priority values get translated into constraints. For a label with an intrinsic content size of { 100, 30 }, horizontal/vertical compression resistance priority of 750, and horizontal/vertical content hugging priority of 250, four constraints will be generated:

H:[label(<=100@250)] 
H:[label(>=100@750)]
V:[label(<=30@250)]
V:[label(>=30@750)]

请参考:

  • iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级
  • iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority
  • What is the content compression resistance and content hugging of a UIView?
  • Cocoa Autolayout: content hugging vs content compression resistance priority

以下内容来自:Xcode的Content Hugging 和 Content Compression Resistance

UIView中关于Content Hugging 和 Content Compression Resistance的方法有:

- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis

大概的意思就是设置优先级的。
Hugging priority 确定view有多大的优先级阻止自己变大。
Compression Resistance priority确定有多大的优先级阻止自己变小。
很抽象,其实content Hugging就是要维持当前view在它的optimal size(intrinsic content size),可以想象成给view添加了一个额外的width constraint,此constraint试图保持view的size不让其变大:
view.width <= optimal size
此constraint的优先级就是通过上面的方法得到和设置的,content Hugging默认为250.
Content Compression Resistance就是要维持当前view在他的optimal size(intrinsic content size),可以想象成给view添加了一个额外的width constraint,此constraint试图保持view的size不让其变小:
view.width >= optimal size
此默认优先级为750.

还是很抽象,好吧,下面举个例子就明白了:

button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.translatesAutoresizingMaskIntoConstraints = NO;
[button1 setTitle:@"button 1 button 2" forState:UIControlStateNormal];
[button1 setBackgroundColor:[UIColor redColor]];
[button1 sizeToFit];

[self.view addSubview:button1];

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-150.0f];
constraint.priority = 751.0f; //其实这里不用设置为751,constraint默认的priority为UILayoutPriorityRequired,值为1000

[self.view addConstraint:constraint];

AutoLayout与Size Class相关_第1张图片

当我们把751变为749的时候,效果如下:
AutoLayout与Size Class相关_第2张图片


WWDC 2012 Session笔记——202, 228, 232 AutoLayout(自动布局)入门

在创建约束之后,需要将其添加到作用的view上。UIView(当然NSView也一样)加入了一个新的实例方法:

  • -(void)addConstraint:(NSLayoutConstraint *)constraint; 用来将约束添加到view。在添加时唯一要注意的是添加的目标view要遵循以下规则:
  • 对于两个同层级view之间的约束关系,添加到他们的父view上
    AutoLayout与Size Class相关_第3张图片

  • 对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上
    AutoLayout与Size Class相关_第4张图片

  • 对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上
    AutoLayout与Size Class相关_第5张图片

刷新
可以通过-setNeedsUpdateConstraints和-layoutIfNeeded两个方法来刷新约束的改变,使UIView重新布局。这和CoreGraphic的-setNeedsDisplay一套东西是一样的~

你可能感兴趣的:(iOS,布局)