zoukankan      html  css  js  c++  java
  • iOS Masonry 抗压缩 抗拉伸

    约束优先级: 在Autolayout中每个约束都有一个优先级, 优先级的范围是1 ~ 1000。创建一个约束,默认的优先级是最高的1000
    Content Hugging Priority: 该优先级表示一个控件抗被拉伸的优先级。优先级越高,越不容易被拉伸,默认是250。
    Content Compression Resistance Priority: 该优先级和上面那个优先级相对应,表示一个控件抗压缩的优先级。优先级越高,越不容易被压缩,默认是750

    有这样一个cell,底部品牌的长度是不固定的,右边的车系也是不固定,并且车系的高度会自动换行,numberOfLines=0;针对这样的cell布局,若果正常布局,达到的效果如下:

    布局部分代码:

       [self.brandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.productNameLb.mas_left);
            make.top.mas_equalTo(lineView.mas_bottom).mas_offset(10);
        }];
    
        [self.carTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(lineView.mas_bottom).mas_offset(10);
            make.left.mas_equalTo(self.brandLabel.mas_right).mas_offset(20);
            make.right.mas_offset(-10);
            make.bottom.mas_offset(-10);
        }];
    

    很明显这里的布局属于约束冲突的情况,想要解决这样的情况方法也有多种,这里介绍一种autolayout里面有一个叫做抗压缩 抗拉伸的概念 方法如下:

    • (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
      - (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
      - (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
      - (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

    然后回到我们这次的问题,很明显右边的内容过多导致左边内容显示不全,那么利用左边抗压缩的概念,提高左边label水平方向的抗压缩值,右边label提高垂直方向抗压缩值 代码如果

        [self.brandLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];  //设置水平方向抗压缩优先级高 水平方向可以正常显示
        [self.carTypeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];  //设置垂直方向挤压缩优先级高 垂直方向可以正常显示
    

    设置了抗压缩后达到了效果如下:

    转载请标注来源 http://www.cnblogs.com/qqcc1388/p/9044927.html

  • 相关阅读:
    DM数据库disql的使用 Disql disql 达梦数据库Disql
    移动端禁止蒙层下的页面滚动
    移动端如何自动适配px
    使用Vant做移动端对图片预览ImagePreview和List的理解
    uniapp中使用uView组件库
    h5使用vuephotopreview 做全屏预览
    jsonview的实现
    PC端自适应使用rem 移动端适配升级版
    axios解决跨域问题(vuecli3.0)
    vs code 配置git path
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/9044927.html
Copyright © 2011-2022 走看看