Masonry 使用注意

1,如何打印 frame

    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view];
    
    [view  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(20);
    }];
    
    [view layoutIfNeeded];
    NSLog(@"%f",view.yj_height);

2,UIScrollView 如何 使用 masnory

    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor orangeColor];
    scrollView.pagingEnabled =YES;
    // 添加scrollView添加到父视图,并设置其约束
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(100);
    }];
    // 设置scrollView的子视图,即过渡视图contentSize,并设置其约束
    UIView *contentView = [[UIView alloc]init];
    [scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.height.equalTo(scrollView);
    }];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor purpleColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:button];

    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(scrollView);
        make.width.equalTo(scrollView);
        make.left.mas_equalTo(0);
    }];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor blueColor];
    [scrollView addSubview: view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(scrollView);
        make.width.equalTo(scrollView);
        make.left.equalTo(button.mas_right);
    }];
    // 设置过渡视图的右距(此设置将影响到scrollView的contentSize)
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view.mas_right);
    }];
    

3,利用 masonry 使tableview cell 高度自适应文本

1>
// 必须要给cell高度估计值
_table.estimatedRowHeight = 175;

2>

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 return 60;
}

这个方法不要设置

3>在cell 中用masonry设置

    UILabel *titleLab  = [[UILabel alloc]initWithFrame:CGRectZero];
    titleLab.text = @"姓名sasaadsasdmmm姓名asaadsasdmm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasdm姓名asaadsasd";
    titleLab.textColor = UIColorFromRGB(0x4b4b4b);
    titleLab.textAlignment = NSTextAlignmentLeft;
    titleLab.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:titleLab];
    titleLab.numberOfLines = 0;
    
    _titleLab = titleLab;


    CGFloat  kPadding = 10;
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(kPadding);
        make.left.mas_equalTo(kPadding);
        make.right.mas_equalTo(-kPadding);
        make.bottom.mas_equalTo(-kPadding);
        
        
    }];

注意这样写 不能在layoutSubviews 方法中设置 layout!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 个人Github博客,求关注 1 理解自身内容尺寸约束与抗压抗拉 自身内容尺寸约束:一般来说,要确定一个视图的精确...
    宿于松下阅读 6,769评论 0 5
  • Github 简要 自动布局最重要的是约束:UI元素间关系的数学表达式。约束包括尺寸、由优先级和阈值管理的相对位置...
    码码乐趣阅读 43,518评论 24 123
  • 一 常用方法介绍 masonry 自动帮我们把 autoresizing给禁用掉 这两个宏定义要放到导入头文件之前...
    dicesc阅读 3,808评论 0 1
  • 喷淋:包括负一层湿式报警阀(喷淋系统有水),一层至八层预作用阀+湿式报警阀组成(喷淋系统无水)。 1.负一层湿式阀...
    卫伟阅读 4,799评论 0 0
  • 拍照的时候稍微用滤镜修饰了一下~
    辛十四娘阅读 1,228评论 0 0