Skip to content

Instantly share code, notes, and snippets.

@jonahb
Created February 7, 2019 04:37
Show Gist options
  • Save jonahb/d4cc902658069a27bc178d9f1c3aed36 to your computer and use it in GitHub Desktop.
Save jonahb/d4cc902658069a27bc178d9f1c3aed36 to your computer and use it in GitHub Desktop.
//
// NibBackedView.swift
// Coop
//
// Created by Jonah Burke on 1/8/19.
// Copyright © 2019 Jonah Burke. All rights reserved.
//
import UIKit
protocol NibBackedView where Self: UIView {
var contents: UIView? { get }
var backingNib: UINib { get }
}
extension NibBackedView {
var backingNib: UINib {
let name = String(describing: type(of: self)) + "Contents"
return UINib(nibName: name, bundle: nil)
}
func loadFromBackingNib() {
backingNib.instantiate(withOwner: self, options: nil)
if let contents = contents {
addSubview(contents)
contents.translatesAutoresizingMaskIntoConstraints = false
contents.align(to: layoutMarginsGuide)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment