[英]SWIFT How to make a UIButton's right side appears first then the left side
[英]Creating UIButton in Xcode with icon on right side and text on left side
我正在嘗試在 Swift 中創建一個UIButton
,其中標簽距左側 20 像素,圖標(SF 符號)距右側 20 像素,請參閱附圖。
我為您創建了這個自定義類(在聲明按鈕時,請編寫 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.