Last active
August 16, 2016 13:20
-
-
Save xdmorgan/0b74fdb6f1d232b8b8d2 to your computer and use it in GitHub Desktop.
Swift 2 IBDesignable Nib Tutorial / Boilerplate
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
/* | |
Start by creating two files (replace NIBEXAMPLE with w/e you want but make sure the names match): | |
1, NIBEXAMPLE.xib | |
* File > New > File... > iOS > View | |
2, NIBEXAMPLE.swift | |
* File > New > File... > iOS > Cocoa Touch Class (Extends UIView) | |
After creating the two files above, replace the default contents of your .swift file | |
with the code below. Be sure to update `class NIBEXAMPLE` and `nibName = "NIBEXAMPLE"` | |
to whatever you named your files. | |
Open the .xib file in interface builder, click `File's Owner` and set the custom class to | |
NIBEXAMPLE (via identity inspector). Doing this will associate the swift class with the | |
view file. You can now open the two files side-by-side and ctrl+drag to create IBOutlets. | |
Use `setup()` to define any custom logic which should be performed when the nib is created. | |
Usage => | |
Interface Builder: | |
Create a View then set the custom class to NIBEXAMPLE (in identity inspector) | |
Programmatically: | |
let nibExample = NIBEXAMPLE( | |
frame: CGRect( | |
x: 0, | |
y: 0, | |
width: self.view.frame.width, | |
height: self.view.frame.height | |
) | |
) | |
self.view.addSubview(nibExample) | |
*/ | |
import UIKit | |
@IBDesignable | |
class NIBEXAMPLE: UIView { | |
// ====================================================================== | |
// MARK: - Vars | |
// ====================================================================== | |
// immutables | |
let nibName = "NIBEXAMPLE" | |
// mutables | |
var view:UIView! | |
// ====================================================================== | |
// MARK: - Lifecycle | |
// ====================================================================== | |
override init(frame: CGRect) | |
{ | |
super.init(frame: frame) | |
self.prepareNib() | |
} | |
required init?(coder aDecoder: NSCoder) | |
{ | |
super.init(coder: aDecoder) | |
self.prepareNib() | |
} | |
func prepareNib() -> Void | |
{ | |
// get the nib, add to screen | |
self.view = loadViewFromNib() | |
self.view.frame = self.bounds | |
self.view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] | |
self.addSubview(self.view) | |
// nib now ready for use | |
self.setup() | |
} | |
func loadViewFromNib() -> UIView | |
{ | |
let bundle = NSBundle(forClass: self.dynamicType) | |
let nib = UINib(nibName: self.nibName, bundle: bundle) | |
let xview = nib.instantiateWithOwner(self, options: nil)[0] as! UIView | |
return xview | |
} | |
// ====================================================================== | |
// MARK: - Setup | |
// ====================================================================== | |
func setup() -> Void | |
{ | |
// nib is ready to use | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment