Created
February 7, 2019 04:37
-
-
Save jonahb/d4cc902658069a27bc178d9f1c3aed36 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// 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