2014-08-27 70 views
0

我想創建一個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)) 

    } 


} 

故事板視圖(錯誤)

storyboard view

模擬器視圖(正確)

simulator view

回答

1

你實際上並沒有做任何的核芯顯卡在drawRect()法繪製。要麼進行一些繪圖到當前的CGContextRef,要麼將代碼移動到某個地方,如layoutSubviews()