Created
January 13, 2020 14:41
-
-
Save letatas/2eaf0dafeba076cad4b6b5cb4b9fda80 to your computer and use it in GitHub Desktop.
ReusableXibViews - NibWrapperView
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
// | |
// 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