2016-01-29 35 views

回答

1

您可以繼承UIButton,然後基於UIControlState進行繪製。例如:

class CustomButton: UIButton { 
    override var selected: Bool { 
     didSet { 
      self.setNeedsDisplay() 
     } 
    } 

    override var highlighted:Bool{ 
     didSet { 
      self.setNeedsDisplay() 
     } 
    } 

    override func drawRect(rect: CGRect) { 
     if self.state == .Selected 
     { 
      //Do complex vector drawing for selected state 
      let roundRect = UIBezierPath(roundedRect: self.bounds, cornerRadius: 15.0) 
      UIColor.greenColor().setFill() 
      roundRect.fill() 
     } 
     else if self.state == .Normal 
     { 
      //Do complex vector drawing for normal state 
      let rect = UIBezierPath(rect: self.bounds) 
      UIColor.redColor().setFill() 
      rect.fill() 
     } 
     else if self.state == .Highlighted 
     { 
      //Do complex vector drawing for highlighted state 
      let roundRect = UIBezierPath(roundedRect: self.bounds, cornerRadius: 15.0) 
      UIColor.greenColor().setFill() 
      roundRect.fill() 
     } 
    } 
} 

當正常顯示時顯示紅色矩形,當選擇或高亮顯示時顯示綠色圓形矩形。

+0

謝謝@beyowulf。你碰巧知道如何調整IB的貝塞爾形狀?我無法設定正確的高度。 – TheoK

+1

將UIViewContentMode設置爲.Redraw,並設置其框架或添加高度約束並將其設置爲所需值。 – beyowulf

相關問題