Created
March 11, 2016 20:34
-
-
Save Blackjacx/c265534d5219e28f9147 to your computer and use it in GitHub Desktop.
NibLoadableView.swift
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
import UIKit | |
class NibLoadableView: UIView { | |
@IBOutlet var view: UIView! | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
let className = self.dynamicType | |
let nibName = NSStringFromClass(className).componentsSeparatedByString(".").last! | |
UINib(nibName: nibName, bundle: nil).instantiateWithOwner(nil, options: nil) | |
let views = ["subview":view] | |
self.addSubview(view) | |
view.translatesAutoresizingMaskIntoConstraints = false | |
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview]|", options: [], metrics: nil, views: views)) | |
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]|", options: [], metrics: nil, views: views)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! I had to change nil to self in UINib(nibName: nibName, bundle: nil).instantiateWithOwner(self, options: nil)