Xcode8 + and iOS10 + use Masonry to automatically calculate the Row Height, xcode8masonry

Source: Internet
Author: User

Xcode8 + and iOS10 + use Masonry to automatically calculate the Row Height, xcode8masonry

When talking about the automatic calculation Row Height of tableView, I really don't want to mention it any more. I don't know how many times it has been written. But it's such a little thing that can't be done if it's hard for me. It's just getting lost.

1. Set the estimated Row Height and Row Height of tableView to automatically calculate

1 // set the predicted Row Height 2 self. tableView. estimatedRowHeight = 200; 3 // set the Row Height to automatically calculate 4 self. tableView. rowHeight = UITableViewAutomaticDimension;

2. Set the bottom constraint of the cell contentView to align with the bottom constraint of the bottom control.

1-(void) setupUI {2 UIImageView * imgV = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "1"]; 3 UILabel * rightLabel = [[UILabel alloc] init]; 4 UILabel * bottomLabel = [[UILabel alloc] init]; 5 6 [self. contentView addSubview: imgV]; 7 [self. contentView addSubview: rightLabel]; 8 [self. contentView addSubview: bottomLabel]; 9 _ imgV = imgV; 10 _ rightLabel = rightLabel; 11 _ bottomLabel = bottomLabel; 12 13 imgV. contentMode = UIViewContentModeScaleAspectFit; 14 rightLabel. text = @ "I am the Label on the right of the image"; 15 bottomLabel. text = @ "Label under the image"; 16 17 [imgV mas_makeConstraints: ^ (MASConstraintMaker * make) {18 make. top. left. similar to (self. contentView ). offset (10); 19 make. size. mas_shortto (CGSizeMake (100,100); 20}]; 21 [rightLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {22 make. top. similar to (imgV); 23 make. left. similar to (imgV. mas_right ). offset (10); 24}]; 25 [bottomLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {26 make. top. similar to (imgV. mas_bottom ). offset (15); 27 make. left. similar to (imgV ). offset (30); 28 make. height. mas_defaults to (20); 29}]; 30 [self. contentView mas_makeConstraints: ^ (MASConstraintMaker * make) {31 make. top. left. right. mas_align to (self); 32 // align the bottom and bottom of contentView with 33 make. bottom. failed to (bottomLabel); 34}]; 35}

3. Check, view, and view. The following error occurs:

 

Please refer to the important part. The contentView height is actually 0. What is the case? How can I report an error when I write code hundreds of times? I am so sorry for this.

After studying for a long time, I finally found out that Xcode8.0 + and iOS10 + problems exist.

Modify to: Align the bottom of the bottom control with the bottom of contentView, and then align the four sides of contentView with self.

[BottomLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {make. top. similar to (imgV. mas_bottom ). offset (15); make. left. similar to (imgV ). offset (30); make. height. mas_align to (20); // key code: make. bottom. similar to (self. contentView) ;}]; [self. contentView mas_makeConstraints: ^ (MASConstraintMaker * make) {// key code: make. top. left. right. bottom. mas_pointer to (self);}];

 

That's all. You think too much about it. The running result is as follows:

 

Why? The height of contentView conflicts with that of controls. I also want to know. However, I don't know the reason. I only know how to solve the problem. If you have any reason, you can post a comment and share it with me.

Here I need to quote a paragraph of NB_Token, which is the middleware and solved the problem for me.

Source: ranking (high, medium, low. The default priority is medium, so when we repeat the constraints on a control, a warning is printed, telling us to fix them. Now that you know the cause of the warning, there are two solutions: 1. Find the control and modify its constraints to eliminate the warning information. 2. Set the control's constraint priority to advanced, so there will be no warning even if the constraint is repeated. This is also the simplest way to save trouble.

 

In cell, as long as you set the height of the control (including the size), it will conflict. We need to increase the priority of the constraint.

The following is the final code:

1-(void) setupUI {2 UIImageView * imgV = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "1"]; 3 UILabel * rightLabel = [[UILabel alloc] init]; 4 UILabel * bottomLabel = [[UILabel alloc] init]; 5 6 [self. contentView addSubview: imgV]; 7 [self. contentView addSubview: rightLabel]; 8 [self. contentView addSubview: bottomLabel]; 9 _ imgV = imgV; 10 _ rightLabel = rightLabel; 11 _ bottomLabel = bottomLabel; 12 13 imgV. contentMode = UIViewContentModeScaleAspectFit; 14 rightLabel. text = @ "I am the Label on the right of the image"; 15 bottomLabel. text = @ "Label under the image"; 16 17 [imgV mas_makeConstraints: ^ (MASConstraintMaker * make) {18 make. top. left. similar to (self. contentView ). offset (10); 19 // key code: Raise the constraint priority by 20 make. size. mas_intent to (CGSizeMake (100,100 )). priorityHigh (); 21}]; 22 [rightLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {23 make. top. similar to (imgV); 24 make. left. similar to (imgV. mas_right ). offset (10); 25}]; 26 [bottomLabel mas_makeConstraints: ^ (MASConstraintMaker * make) {27 make. top. similar to (imgV. mas_bottom ). offset (15); 28 make. left. similar to (imgV ). offset (30); 29 make. height. mas_align to (20); 30 // key code: Set the bottom constraint of the bottom control and the bottom alignment of contentView 31 make. bottom. similar to (self. contentView); 32}]; 33 [self. contentView mas_makeConstraints: ^ (MASConstraintMaker * make) {34 // key code: the four sides of contentView and self Alignment 35 make. top. left. right. bottom. mas_pointer to (self); 36}]; 37}

Share:

 

Hope that our friends who solve the problem through this article will give lianxi_^

 

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.