Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DenTelezhkin
Last active July 4, 2020 17:47
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DenTelezhkin/c3d682e7f499113109d6 to your computer and use it in GitHub Desktop.
Save DenTelezhkin/c3d682e7f499113109d6 to your computer and use it in GitHub Desktop.
DEPRECATED. Please use https://github.com/MLSDev/LoadableViews. Create reusable views in XIBs and easily load them in another XIB, storyboard, or programmatically.
import Foundation
import UIKit
protocol NibDefinable {
var nibName: String { get }
}
extension NibDefinable {
var nibName : String {
return String(self.dynamicType)
}
}
class LoadableFromXibView: UIView, NibDefinable {
@IBOutlet weak 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 = [.FlexibleWidth, .FlexibleHeight]
addSubview(view)
}
private func loadViewFromXib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
return view
}
}
@winkelsdorf
Copy link

Great snippet! Just a quick note: if nibName is fully implemented in the protocol extension, an implementation in a subclass of LoadableFromXibView will never get called. Just ran into this.

So better implement this in the LoadableFromXibView:

protocol NibDefinable {
    var nibName: String { get }
}

@IBDesignable
class LoadableFromXibView: UIView, NibDefinable {

    @IBOutlet weak var view: UIView!

    var nibName: String {
        return String(self.dynamicType)
    }

Child class implements

class ChildView: ParentLoadableFromXibView {

    override var nibName: String {
        return String(ParentLoadableFormXibView.self) // use parent's nib
    }

Works like a charm :)

Another way could be iterating over the parent classes in loadViewFromXib until a nib could be loaded, but .. no, that's too much for this.

@oblq
Copy link

oblq commented May 13, 2016

What's the deal on using protocol and protocol extension for nibName?
This is my simplyfied version and it work great.
It also sendSubviewToBack(view) because if you add some views under it on storyboard they goes under the LoadableFromXibView when you run the project.

public class LoadableFromXibView: UIView {

@IBOutlet weak var view : UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    xibSetup()
}

private func xibSetup() {
    view = loadViewFromXib()
    view.frame = bounds
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    addSubview(view)
    sendSubviewToBack(view)
}

private func loadViewFromXib() -> UIView {
    let nib = UINib(nibName: String(self.dynamicType), bundle: NSBundle(forClass: self.dynamicType))
    if let cView = nib.instantiateWithOwner(self, options: nil).first as? UIView {
        return cView
    }
    else {
        print("Unable to load custom view!")
        return UIView()
    }
}

}

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