Autolayout - intrinsic size of UIButton does not include title insets

前端 未结 12 1074
不知归路
不知归路 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 07:47

    And for Swift worked this:

    extension UIButton {
        override open var intrinsicContentSize: CGSize {
            let intrinsicContentSize = super.intrinsicContentSize
    
            let adjustedWidth = intrinsicContentSize.width + titleEdgeInsets.left + titleEdgeInsets.right
            let adjustedHeight = intrinsicContentSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom
    
            return CGSize(width: adjustedWidth, height: adjustedHeight)
        }
    }
    

    Love U Swift

提交回复
热议问题