Skip to content

Instantly share code, notes, and snippets.

@isaac-weisberg
Created June 9, 2018 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaac-weisberg/6228c933b99f8e034396c9c6a662e313 to your computer and use it in GitHub Desktop.
Save isaac-weisberg/6228c933b99f8e034396c9c6a662e313 to your computer and use it in GitHub Desktop.
Load your views from Xibs with this
//
// 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