Search code examples
iosswiftxib

programmatically customize UIView in xib


I've created a Xib file with a UIView and then created a custom UIView class connected to the xibs file owner. However i'm not sure how to create something programatically. I've tried to add the awakeFromNib function however it seems not ro run, what method should implement in order to change backgroundColor programmatically?

func loadViewFromNib() -> UIView {

    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "MediaPlayer", bundle: bundle)
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

    return view
}

Solution

  • awakeFromNib will be called only if call UINib like this: https://stackoverflow.com/a/25513605/846780