Skip to content

Instantly share code, notes, and snippets.

@isaac-weisberg
Created June 8, 2018 02:44
Show Gist options
  • Save isaac-weisberg/1cb1ce67c73604c592039988a64254dd to your computer and use it in GitHub Desktop.
Save isaac-weisberg/1cb1ce67c73604c592039988a64254dd to your computer and use it in GitHub Desktop.
Xib Modular View. Extend this class to load contents of your custom views from .nibs.
//
// XibModularView.swift
// Pobjects
//
// Created by Isaac Weisberg on 4/5/18.
// Copyright © 2018 Isaac Weisberg. All rights reserved.
//
import UIKit
class XibModularView: UIView {
var xibContentView: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xib()
}
override init(frame: CGRect) {
super.init(frame: frame)
xib()
}
private func xib() {
xibContentView = UINib(nibName: "\(self.classForCoder)", bundle: .main).instantiate(withOwner: self, options: nil).first as? UIView
guard xibContentView != nil else {
return
}
xibContentView.frame = self.bounds
addSubview(xibContentView)
}
}
class XibModularTableViewCell: UITableViewCell {
var xibContentView: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xib()
}
private func xib() {
xibContentView = UINib(nibName: "\(self.classForCoder)", bundle: .main).instantiate(withOwner: self, options: nil).first as? UIView
guard xibContentView != nil else {
return
}
xibContentView.frame = self.bounds
contentView.addSubview(xibContentView)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment