📜  按钮 sizetofit 不工作 swift 代码示例

📅  最后修改于: 2022-03-11 15:00:59.854000             🧑  作者: Mango

代码示例1
class ResizableButton: UIButton {
    override var intrinsicContentSize: CGSize {
       let imageViewWidth = imageView?.frame.width ?? 0.0
       let labelSize = titleLabel?.sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude)) ?? .zero
       let desiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right + imageViewWidth, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom)

       return desiredButtonSize
    }
}