In Autolayout, note the use of Hugging and Compression: autolayouthugging

Source: Internet
Author: User

In Autolayout, note the use of Hugging and Compression: autolayouthugging

Preface

This article focuses onAutolayoutAddintrinsicSizeAttribute control constraints.

From my blog, welcome To: To Be Independent.

 

Hugging and Compression attributes

There are many articles about these two concepts, such as Cocoa Autolayout: content hugging vs content compression resistance priority on stackoverflow. I think it clearly shows how to use the settings, but there is still a lack of the time to use it, that is, the relationship with the `intrinsicSize. Let's take a look at the instructions in the following document:

- contentCompressionResistancePriorityForAxis://Returns the priority with which a view resists being made smaller than its intrinsic size.- contentHuggingPriorityForAxis://Returns the priority with which a view resists being made larger than its intrinsic size.

 

From this perspective, it is clear that for controls with the intrinsicSize attribute (such as UILabel and UIButton), if the current frame is larger than the displayed content range, the set Hugging attribute takes effect, otherwise, the configured Compression attribute takes effect. A greater value indicates a higher priority, indicating that the current attribute is dominant. To put it simply, the greater the value of the Hugging attribute (the higher the priority), the control needs to be compact.
The values of Hugging and Compression attributes have default values:

Why are there different values? Let's take a look at the value of UILayoutPriority:

static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = 1000; // A required constraint.  Do not exceed this.static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = 750; // This is the priority level with which a button resists compressing its content.static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = 250; // This is the priority level at which a button hugs its contents horizontally.static const UILayoutPriority UILayoutPriorityFittingSizeLevel NS_AVAILABLE_IOS(6_0) = 50;// When you send -[UIView systemLayoutSizeFittingSize:], the size fitting most closely to the target size (the argument) is computed.  UILayoutPriorityFittingSizeLevel is the priority level with which the view wants to conform to the target size in that computation.  It's quite low.  It is generally not appropriate to make a constraint at exactly this priority.  You want to be higher or lower.

 

It can be seen that during the design, iOS developers considered that the control similar to UILabel should first display all the content.

Programming implementation

Load a button using a piece of code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];button.translatesAutoresizingMaskIntoConstraints = NO;button.backgroundColor = [UIColor redColor];[button setTitle:@"a long long title" forState:UIControlStateNormal];[self.view addSubview:button];NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];[self.view addConstraint:constraint];constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];[self.view addConstraint:constraint];constraint = [[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:0 constant:50.0f];[self.view addConstraint:constraint];

 

The constraint added above makes the frame of the button insufficient to display the title content. Note that the default priority of the constraint is UILayoutPriorityRequired. Therefore, we can modify the constraint of the last width:

constraint.priority = UILayoutPriorityDefaultHigh - 1;

 

You can also use a similar method to modify controls added with SB. Why is the value of the Hugging attribute in SB 251 After a constraint is added to the UILable control added to SB? This is to display the full content by default. At this point, you can manually reduce the space size in sb, and then set the constriant (width or tailing) Priority of a property of the control to low. At this time, you can also find the constraints in SB from the blue solid line to the blue dotted line. Of course, if the compression constraint works, the constraint is also a blue dotted line.

Used with other controls

The preceding single control can be used normally. If an adjacent control is set, what should I pay attention? The answer is NO. You don't need to worry about anything. You still add constraints according to the previous method, which greatly simplifies the workload.
In addition, it should be noted that when you need to update the text on the control, in order to have a better animation effect, you need:

[label.superView layoutIfNeeded];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.