Created
May 31, 2016 15:25
-
-
Save DamienBell/72cc804d05934ea4998d555fea0faa0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DBNibLoadingView.swift | |
// Created by Damien Bell on 2/15/16. | |
// Copyright © 2016 Damien Bell. All rights reserved. | |
// | |
//How To Use: | |
//Create a nib file where the name of the file matches the class name | |
//set the xib's files owner to the relevant class. Important! set files owner and not the UIView class on the xib object itself | |
//Using this approach you can still set iboutlets and properties as normal | |
import UIKit | |
class DBNibLoadingView: UIView { | |
var view: UIView! | |
override init(frame: CGRect){ | |
super.init(frame: frame) | |
xibSetup() | |
} | |
required init?(coder aDecoder: NSCoder){ | |
super.init(coder: aDecoder) | |
xibSetup() | |
} | |
func xibSetup(){ | |
view = loadViewFromXib() | |
view.frame = bounds | |
view?.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] | |
addSubview(view) | |
} | |
func loadViewFromXib()->UIView{ | |
let bundle = NSBundle(forClass: self.dynamicType) | |
let nib = UINib(nibName: String(self.dynamicType), bundle: bundle) | |
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView | |
return view | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Subclass of UIView to make loading views from nibs less tedious.
How To Use:
-Create a nib file where the name of the file matches the class name
-Set the xib's files owner to the relevant class. Important! set files owner and not the UIView class on the xib object itself