• Masonry 与 frame 混用导致的问题


    https://www.jianshu.com/p/357fab4b84e7

    Masonry frame 混用可能出现子控件大小跟预期不一致的情况,具体是什么样呢?

    例如,自定义一个 UIView 的子类SmallViewSmallView 中含有一个 UIImageView,在控制器创建SmallView,在SmallView创建UIImageView时都使用 init 方法,都没有给定 frame,但是在使用 masonry 约束时,用到了 frame,那么这就很可能会出现问题,导致大小跟预期不一致。

    .

    SmallView 中创建 UIImageView

    _imageView = [[UIImageView alloc] init];

    _imageView.image = [UIImage imageNamed:@"一无所有"];

    [self addSubview:_imageView];

    //1.

    //这种写法容易出问题,如果 SmallView 创建时没有设置 frame 或者重新约束,那么_imageView并不能达到预期的效果

    //SmallView init 会先执行这里,然后在约束SmallView,但是 imageview 的大小已经在这里定型了

    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {

        //问题就在self.bounds.size.width,self.bounds.size.height,如果外面创建SmallView,没有 frame, 这里就是0

        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width, self.bounds.size.height));

        make.center.mas_equalTo(self);

    }];

    在控制器中创建 SmallView

    //1.没有给定 frame,此时 SmallViewimageView 已经大小为0

    SmallView *small = [[SmallView alloc] init];

    [self.view addSubview:small];

    //约束和 frame 不一致,SmallViewimageView 已经大小为0,只对SmallView起效果了

    [small mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.size.mas_equalTo(CGSizeMake(100, 100));

        make.top.mas_equalTo(self.view).mas_offset(30);

        make.leading.mas_equalTo(self.view).mas_offset(30);

    }];

    结果是这样,没有大小,不是与 SmallView 一样大


     

    imageView 大小为0.png

    那就给一个 frame

    SmallView *small = [[SmallView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    图片大小为 init 时的50,50


     

    image 并没有跟 SmallView 一样大.png

    那么正确的姿势来了。

    .

    SmallView 中创建 UIImageView

    //2.

    //这种写法无论 SmallView在创建时有没有给定 frame 或者给了 frame 但是约束跟 frame 不一致也不会出问题.

    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {

        

        //这里是约束,并不是 frame

        //make.size.mas_equalTo(self);

        make.width.mas_equalTo(self.mas_width);

        make.height.mas_equalTo(self.mas_height);

        make.center.mas_equalTo(self);

    }];

    在控制器中创建 SmallView

    //2.frame 和约束不一致,使用的是约束,无所谓啦

    //SmallView *small = [[SmallView alloc] init];

    SmallView *small = [[SmallView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

    [self.view addSubview:small];

    //约束和 frame 不一致,使用的是约束,无所谓啦

    [small mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.size.mas_equalTo(CGSizeMake(100, 100));

        make.top.mas_equalTo(self.view).mas_offset(30);

        make.leading.mas_equalTo(self.view).mas_offset(30);

    }];

     

    不怕不怕啦.png

    那么,结论就是在自定义控件时,内部约束尽量使用mas_width这种属性,谨慎使用具体数值,如固定的 width 等等。如果你使用了 UIView 的快速改变 frame 的分类一定要注意,很多属性名类似,如 centerX mas_centerX可是不一样的,后果你遇到就知道啦~

    代码https://github.com/lxszl/UIViewTest

    作者:loyt

    链接:https://www.jianshu.com/p/357fab4b84e7

    来源:简书

    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 相关阅读:
    octotree神器 For Github and GitLab 火狐插件
    实用篇如何使用github(本地、远程)满足基本需求
    PPA(Personal Package Archives)简介、兴起、使用
    Sourse Insight使用过程中的常使用功能简介
    Sourse Insight使用教程及常见的问题解决办法
    github 遇到Permanently added the RSA host key for IP address '192.30.252.128' to the list of known hosts问题解决
    二叉查找树的C语言实现(一)
    初识内核链表
    container_of 和 offsetof 宏详解
    用双向链表实现一个栈
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10572360.html
Copyright © 2020-2023  润新知