簡體   English   中英

如何在圖標和外框下創建帶有標題的按鈕

[英]how to create button with title under icon and outside frame

我想和喜歡的圖像創建按鈕,

這是我的代碼,但其他型號的iPhone並非如此:

extension ButtonWithImage {
func alignVertical(spacing: CGFloat = 6.0) {
    guard let imageSize = self.imageView?.image?.size else { return }
    guard let titleSize = titleLabel?.intrinsicContentSize else{return}

    // Title Edge Insets
    let titleEdgeInsets = UIEdgeInsets(top: 0.0 ,
                                       left: -imageSize.width,
                                       bottom: -(self.frame.size.height + 2*spacing),
                                       right: 0.0)
    self.titleEdgeInsets = titleEdgeInsets

    self.imageEdgeInsets = UIEdgeInsets(top: 0.0,
                                        left: 0.0,
                                        bottom: 0.0,
                                        right: -titleSize.width)

    self.contentEdgeInsets = UIEdgeInsets(top: spacing,//edgeOffset,
                                          left: 0.0,
                                          bottom: spacing,//edgeOffset,
                                          right: 0.0)
}
}

在任何型號的iPhone上,titleEdgeInsets中的bottom值是多少?

請使用以下擴展名以方便使用

extension UIButton {

    func alignCenter(withPadding: CGFloat = 6.0) {
        guard
            let imageViewSize = self.imageView?.frame.size,
            let titleLabelSize = self.titleLabel?.frame.size else {
            return
        }

        let totalHeight = imageViewSize.height + titleLabelSize.height + padding

        self.imageEdgeInsets = UIEdgeInsets(
            top: -(totalHeight - imageViewSize.height),
            left: 0.0,
            bottom: 0.0,
            right: -titleLabelSize.width
        )

        self.titleEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: -imageViewSize.width,
            bottom: -(totalHeight - titleLabelSize.height),
            right: 0.0
        )

        self.contentEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: 0.0,
            bottom: titleLabelSize.height,
            right: 0.0
        )
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM