Skip to content

Instantly share code, notes, and snippets.

@gangelo
Created December 8, 2016 14:12
Show Gist options
  • Select an option

  • Save gangelo/99839583edbf794b784e2369b2c4c3e4 to your computer and use it in GitHub Desktop.

Select an option

Save gangelo/99839583edbf794b784e2369b2c4c3e4 to your computer and use it in GitHub Desktop.
A designable .xib .swift class example [Swift 3, Xcode 8.1]
//
// UIDesignableXibExample.swift
//
// Created by Gene M. Angelo Jr. on 12/8/16.
// Copyright © 2016 Mohojo Werks LLC. All rights reserved.
//
import UIKit
@IBDesignable class UIDesignableXibExample: UIView {
// MARK: Properties
fileprivate var view: UIView!
fileprivate let nibName: String = "UIDesignableXibExample" // Or, whatever your .xib name is...
override init(frame: CGRect) {
// properties
super.init(frame: frame)
// Set anything that uses view or visible bounds
setup()
}
required init(coder aDecoder: NSCoder) {
// properties
super.init(coder: aDecoder)!
// Setup
setup()
}
fileprivate func setup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
}
fileprivate func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
if let view = bundle.loadNibNamed(self.nibName, owner: self, options: nil)?.first as? UIView {
return view
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment