Last active
July 4, 2020 17:47
-
-
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.
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 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 {
}