簡體   English   中英

使用右側的圖標和左側的文本在 Xcode 中創建 UIButton

[英]Creating UIButton in Xcode with icon on right side and text on left side

我正在嘗試在 Swift 中創建一個UIButton ,其中標簽距左側 20 像素,圖標(SF 符號)距右側 20 像素,請參閱附圖。

圖片來自 Apple COVID-19 應用

我為您創建了這個自定義類(在聲明按鈕時,請編寫 Covid19Button 而不是 UIButton 以便使用它)。


class Covid19Button: UIButton {
   //declare here the objects or variables you want in your custom object
    var label: UILabel!
    var image: UIImageView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
       commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
     commonInit()
    }
    
    func commonInit(){
//Set your objects' frame and other stuff like colors ecc note that here I set random dimensions. The x should be ok however. You can change them as you prefer 
        label = UILabel(frame: CGRect(x: 20, y: 20, width: 100, height: frame.height-40))
        image = UIImageView(frame: CGRect(x: frame.width-20-image.frame.width, y: 20, width: 50, height: frame.height-40))
        
        addSubview(label)
        addSubview(image)
        
    }
    
}


暫無
暫無

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

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