Skip to content

Instantly share code, notes, and snippets.

@NikKovIos
Last active March 23, 2018 23:00
Show Gist options
  • Save NikKovIos/69d8a1d81ac6897ff1d3bdd878ded51a to your computer and use it in GitHub Desktop.
Save NikKovIos/69d8a1d81ac6897ff1d3bdd878ded51a to your computer and use it in GitHub Desktop.
Swift class to initialize UIView from xib
//
// NibInitializableView.swift
// nik-kov.com
//
// Created by Nik Kov on 16.11.17.
// Copyright © 2017 Nik Kov. All rights reserved.
//
/// Just subclass it
@IBDesignable
class NibInitializableView: UIView {
weak var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
nibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
nibSetup()
}
func nibSetup() {
backgroundColor = .clear
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.translatesAutoresizingMaskIntoConstraints = true
addSubview(view)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment