[ios development] Masonry learning

One, import the library

Masonry supports CocoaPods, which can be integrated directly through the podfile. The configuration of cocoapods and Masonry are as follows:
https://www.jianshu.com/p/9e4e36ba8574
If the configuration is completed, the overall required code is as follows:

  1. Terminal input: cd /Users/(computer id)/Desktop/CocoaPodsTest
    //Enter the path of your own project or drag and drop directly
  2. Create a Podfile file: touch Podfile
  3. Edit the name and version of the third-party library you want to import.
    Use vim to edit the Podfile.
    Terminal input: the
    Insert picture description here
    first line of vim Podfile becomes ios 8.0. The
    second line of myapp becomes the name of the project to be added. The
    third line of AFNetworking becomes the required import library. Just delete the last one (to ensure the latest).
  4. Download the library to Xcode and enter in the terminal: pod install

2. Basic use of Masonry

mas_makeConstraints()    添加约束
mas_remakeConstraints()  移除之前的约束,重新添加新的约束
mas_updateConstraints()  更新约束
 
equalTo()       参数是对象类型,一般是视图对象或者mas_width这样的坐标系对象
mas_equalTo()   和上面功能相同,参数可以传递基础数据类型对象,可以理解为比上面的API更强大
 
width()         用来表示宽度,例如代表view的宽度
mas_width()     用来获取宽度的值。和上面的区别在于,一个代表某个坐标系对象,一个用来获取坐标系对象的值

If you want to use without the preceding mas_, just add two lines of macro definition

The basic setting attributes are as follows:

1. Size: width, height, size.
2. Boundary: left, leading, right, trailing, top, bottom, edges.
3. Center point: center, centerX, centerY.
4. Offset: offset, insets, sizeOffset, centerOffset.
5.priority() constraint priority (0~1000), multipler multiplication factor, dividedBy division factor.

There are three size relationships:

1. equalTo, equal.
2.lessThanOrEqualTo, less than or equal to.
3. greaterThanOrEqualTo, greater than or equal to.

Other usage details: https://www.jianshu.com/p/6ab926c6647d

If you write a specific position, the value in offset() becomes self.view.frame.size.height multiplied by the required percentage

Three, source code

  UIImage *first = [UIImage imageNamed:@"works_img1.png"];
        UIImageView *firstimage = [[UIImageView alloc]initWithImage:first];
        [cell addSubview:firstimage];
        [firstimage mas_makeConstraints:^(MASConstraintMaker *make){
    
    
//            make.centerX.equalTo(cell).offset(-100);
            make.top.equalTo(cell).offset(cell.frame.size.height * 0.005);
            make.left.equalTo(cell).offset(0);
            make.right.equalTo(cell).offset(0);
            make.height.equalTo(@200);
            }]

After constraining it, the desired position in the custom cell can achieve the effect as follows (the above text also uses Masonry):
Insert picture description here

Supplement: It is best to use a percentage for comparison, which is *0.5, similar to this, so that it can ensure normal operation when changing models

Guess you like

Origin blog.csdn.net/m0_46110288/article/details/108474989