Autolayout - intrinsic size of UIButton does not include title insets

前端 未结 12 1117
不知归路
不知归路 2020-12-07 07:11

If I have a UIButton arranged using autolayout, its size adjusts nicely to fit its content.

If I set an image as button.image, the instrinsic size again

12条回答
  •  广开言路
    2020-12-07 08:03

    Why not override the intrinsicContentSize method on UIView? For example:

    - (CGSize) intrinsicContentSize
    {
        CGSize s = [super intrinsicContentSize];
    
        return CGSizeMake(s.width + self.titleEdgeInsets.left + self.titleEdgeInsets.right,
                          s.height + self.titleEdgeInsets.top + self.titleEdgeInsets.bottom);
    }
    

    This should tell the autolayout system that it should increase the size of the button to allow for the insets and show the full text. I'm not at my own computer, so I haven't tested this.

提交回复
热议问题