Skip to content

Instantly share code, notes, and snippets.

@letatas
Created January 13, 2020 14:41
Show Gist options
  • Save letatas/2eaf0dafeba076cad4b6b5cb4b9fda80 to your computer and use it in GitHub Desktop.
Save letatas/2eaf0dafeba076cad4b6b5cb4b9fda80 to your computer and use it in GitHub Desktop.
ReusableXibViews - NibWrapperView
//
// NibWrapperView.swift
// ReusableXibViews
//
// Created by Matthias Lamoureux on 13/01/2020.
// Copyright © 2020 QSC. All rights reserved.
//
import UIKit
/// Class used to wrap a view automatically loaded form a nib file
class NibWrapperView<T: UIView>: UIView {
/// The view loaded from the nib
var contentView: T
required init?(coder: NSCoder) {
contentView = T.loadFromNib()
super.init(coder: coder)
prepareContentView()
}
override init(frame: CGRect) {
contentView = T.loadFromNib()
super.init(frame: frame)
prepareContentView()
}
private func prepareContentView() {
contentView.translatesAutoresizingMaskIntoConstraints = false
addSubview(contentView)
contentView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
contentView.topAnchor.constraint(equalTo: topAnchor).isActive = true
contentView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
contentView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
contentView.prepareForInterfaceBuilder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment