Snip20171225_5.png
通常我們設(shè)置的按鈕是圖片在右邊价匠,文字在左翼馆。所以我們需要自定義按鈕來實現(xiàn)我們一些需求. 主要就是控制一下layoutSubviews里面計算一下尺寸
1.繼承UIButton. 然后重寫這幾個方法衬鱼,
2.接著使用改類創(chuàng)建就對象就好
3.可以在init 中設(shè)置一些自己固定的屬性
class CustomBtn: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel?.font = UIFont.systemFont(ofSize: 12)
setTitleColor(UIColor.green, for: UIControlState.normal)
setImage(UIImage(named:"navigationbar_friendattention"), for: UIControlState.normal)
setImage(UIImage(named:"navigationbar_friendattention_highlighted"), for: UIControlState.highlighted)
sizeToFit()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.frame.origin.x = 20
imageView?.frame.origin.x = 20 + titleLabel!.frame.size.width
}
}
擴展
Snip20171225_14.png
代碼留給你嫉父,喜歡和點贊留給我https://gitee.com/lanyingwei/codes/ebx5hj6q3d1vsop7zrmlt98