2015-03-25 54 views
-1

我想知道如何執行UIView的drawRect()被調用後的代碼?什麼後drawRect()

在iOS渲染週期中是否存在afterdrawRect掛鉤/方法?

爲什麼? 我想在InterfaceBuilder中顯示一個按鈕(通過IBDesignable),但將其隱藏在最終產品中。

@IBDesignable class FakeUIButton: UIButton { 

    @IBInspectable var fillColor: UIColor = UIColor.purpleColor() { 
     didSet { 
      layer.backgroundColor = fillColor.CGColor 
      self.alpha = 0.5 
     } 
    } 

    override func drawRect(rect: CGRect) { 
     layer.backgroundColor = fillColor.CGColor 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     self.backgroundColor = UIColor.clearColor() // called to late in render cycle 
    } 

} 
+0

你可以在viewDidLoad中隱藏按鈕 – SolaWing 2015-03-25 11:47:30

+0

你能解釋爲什麼你想在IB中顯示一個按鈕,但在運行時隱藏它嗎? – sbooth 2015-03-25 11:48:05

+0

爲了讓某種「熱點」在後臺創建一個只有jpgs的快速原型。我添加了上面的代碼。 – fabian 2015-03-25 11:53:38

回答

0

您可以使用

prepareForInterfaceBuilder() 

準備您在IB顯示視圖,在此代碼不會在應用程序本身運行。 或者你可以使用

#if !TARGET_INTERFACE_BUILDER 
    // this code will run in the app itself 
#else 
    // this code will execute only in IB 
#endif 

只運行代碼,在IB顯示時。

相關問題