Skip to content

Instantly share code, notes, and snippets.

@Blackjacx
Created March 11, 2016 20:34
Show Gist options
  • Save Blackjacx/c265534d5219e28f9147 to your computer and use it in GitHub Desktop.
Save Blackjacx/c265534d5219e28f9147 to your computer and use it in GitHub Desktop.
NibLoadableView.swift
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))
}
}
@nbasham
Copy link

nbasham commented May 2, 2016

Thank you! I had to change nil to self in UINib(nibName: nibName, bundle: nil).instantiateWithOwner(self, options: nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment