Skip to content

Instantly share code, notes, and snippets.

@DamienBell
Created May 31, 2016 15:25
Show Gist options
  • Save DamienBell/72cc804d05934ea4998d555fea0faa0c to your computer and use it in GitHub Desktop.
Save DamienBell/72cc804d05934ea4998d555fea0faa0c to your computer and use it in GitHub Desktop.
//
// 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
}
}
@DamienBell
Copy link
Author

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

  • Using this approach you can still set iboutlets and properties as usual

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