美文网首页
view的属性intrinsicContentSize的一点探究

view的属性intrinsicContentSize的一点探究

作者: 不辣先生 | 来源:发表于2018-07-18 11:32 被阅读11次

intrinsicContentSize 字面意思是指view的固有大小;
这个属性发挥的空间就是在autolayout布局时会自己计算view的大小,view的子类都可以在autolayout布局时只需要给位置,不需要确定大小,就是因为有它;
所以可以通过继承去重写这个属性去给它一个你想要的大小,拿button举例

   - (instancetype)init
     {
           self = [super init];
           if (self) {
   //不兼容旧版Autoreizingmask,只使用AutoLayout
    //如果为YES,在AutoLayout中则会自动将view的frame和bounds属性转换        为约束。
                  self.translatesAutoresizingMaskIntoConstraints = NO;
            }
         return self;
     }

- (CGSize) intrinsicContentSize {
    
        CGSize s = [super intrinsicContentSize];

        CGFloat w = s.width + self.titleEdgeInsets.left +                          self.titleEdgeInsets.right;
        CGFloat h = s.height + self.titleEdgeInsets.top + > self.titleEdgeInsets.bottom;

        return CGSizeMake(w,h);
     }

可以调整文字的内边距,又不会影响文字的显示,对于UI细节的打磨有一些帮助
当你设置了控件大小这个属性接口的重写就不会生效了,我多次实验的出来的结果,至于原因我也想知道,哈哈

总结

intrinsicContentSize 这个属性是在没有给出控件大小为前提的情况下 ,控价本身计算自己大小的会调用的一个接口,重写这个接口可以实现一些想要的控件内容的操作,另外只对Autolayout布局有效

相关文章

网友评论

      本文标题:view的属性intrinsicContentSize的一点探究

      本文链接:https://www.haomeiwen.com/subject/oiqopftx.html