簡體   English   中英

如何在右側創建帶有圖標的按鈕?

[英]How to create a button with an icon on the right-hand side?

我想在按鈕標簽旁邊放一個圖標。

問題:Interface Builder 沒有將按鈕圖像放在右側。 所以在 (x) 旁邊還有一些空間。

謝謝你的幫助

在此處輸入圖片說明

您可以在 Interface Builder 中通過設置標題和圖像插入來執行此操作:

在此處輸入圖片說明

在這種情況下,我將title right inset設置為 30 ,將image left inset 設置為 80 您可以通過設置UIButtonimageEdgeInsettitleEdgeInset屬性在代碼中實現相同的效果。 知道 UIButton 的子視圖的大小,您可能可以使用以下方法計算邊緣插入:

CGSize labelWidth = myButton.titleLabel.frame.size.width;
CGSize imageWidth = myButton.imageView.frame.size.width;
myButton.titleEdgeInsets = (UIEdgeInsets){0.0, -imageWidth, 0.0, imageWidth};
myButton.imageEdgeInsets = (UIEdgeInsets){0.0, labelWidth, 0.0, -labelWidth};

在 Xcode 8 中,您將在大小檢查器中找到這些屬性:

Xcode 大小檢查器屏幕截圖

在此處輸入圖片說明

在窗口上選擇圖像(如果(x)是圖像)或標題(如果(x)只是關鍵)並根據您的需要設置左,右偏移

我已經做到了:

class CustomButton: UIButton {
    
    open var rightIcon: UIImage? {
        didSet {
            rightIconImage = UIImageView(image: rightIcon?.withRenderingMode(.alwaysTemplate))
            rightIconImage?.tintColor = .white
            if let rightIconImage = rightIconImage {
                addSubview(rightIconImage)
            }
            layoutSubviews()
        }
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        setupRightIconIfNeeded()
    }
    
    func setupRightIconIfNeeded() {
        if let rightIconImage = rightIconImage {
            rightIconImage.translatesAutoresizingMaskIntoConstraints = false
            titleLabel?.translatesAutoresizingMaskIntoConstraints = false
            if let titleLabel = titleLabel, titleLabel.frame.width > 0 {
                let paddingLeft = (frame.width - titleLabel.frame.width - spacing - iconSize) / 2
                NSLayoutConstraint.deactivate(constraints)
                NSLayoutConstraint.deactivate(titleLabel.constraints)
                NSLayoutConstraint.activate([
                    //set height constraint for button
                    heightAnchor.constraint(equalToConstant: apolloSize.height),
                    //set constraint for title label
                    titleLabel.widthAnchor.constraint(equalToConstant: titleLabel.frame.width),
                    titleLabel.heightAnchor.constraint(equalToConstant: titleLabel.frame.height),
                    titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
                    titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: paddingLeft),
                    //set constraint for right icon
                    rightIconImage.leadingAnchor.constraint(equalTo: titleLabel.trailingAnchor, constant: spacing),
                    rightIconImage.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor),
                    rightIconImage.widthAnchor.constraint(equalToConstant: iconSize),
                    rightIconImage.heightAnchor.constraint(equalToConstant: iconSize)
                ])
            }
        }
    }
}

暫無
暫無

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

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