我想創建一個IBDesignable,將一個圖標放在按鈕標籤的右側。 它在模擬器中工作,但故事板不會將圖標重新定位到右側。IBDesignable drawRect不工作?
@IBDesignable class IconLabelButtonView : UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init(coder : NSCoder) {
super.init(coder:coder)
self.setup()
}
func setup() {
}
override func drawRect(rect: CGRect)
{
super.drawRect(rect)
let imageSize:CGSize = self.imageView.frame.size
let titleSize:CGSize = self.titleLabel.frame.size
let labeledIconButtonBufferMagnitude:CGFloat = 6.0
let imagePadding:CGFloat = ceil(labeledIconButtonBufferMagnitude/2)
let titlePadding:CGFloat = floor(labeledIconButtonBufferMagnitude/2)
self.imageEdgeInsets = UIEdgeInsetsMake(0.0, (titleSize.width + imagePadding), 0.0, -(titleSize.width + imagePadding))
self.titleEdgeInsets = UIEdgeInsetsMake(0.0, -(imageSize.width + titlePadding), 0.0, (imageSize.width + titlePadding))
}
}
故事板視圖(錯誤)
模擬器視圖(正確)