Created
June 9, 2018 09:14
-
-
Save isaac-weisberg/6228c933b99f8e034396c9c6a662e313 to your computer and use it in GitHub Desktop.
Load your views from Xibs with this
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
// | |
// XibModularView.swift | |
// Pobjects | |
// | |
// Created by Isaac Weisberg on 4/5/18. | |
// Copyright © 2018 Isaac Weisberg. All rights reserved. | |
// | |
import UIKit | |
protocol XibRenewer: class { | |
var xibView: UIView? { get set } | |
func renewXib() | |
} | |
extension XibRenewer where Self: UIView { | |
func renewXib() { | |
if let xibView = xibView { | |
xibView.removeFromSuperview() | |
} | |
if let view = UINib(nibName: "\(self.classForCoder)", bundle: .main).instantiate(withOwner: self, options: nil).first as? UIView { | |
view.frame = self.bounds | |
addSubview(view) | |
xibView = view | |
} | |
} | |
} | |
class XibModularView: UIView, XibRenewer { | |
var xibView: UIView? | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
renewXib() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
renewXib() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment