Skip to content

Instantly share code, notes, and snippets.

@huynguyencong
Created February 10, 2022 10:20
Show Gist options
  • Save huynguyencong/8ffd95a6b4b8afb1b28aca8c6fd71033 to your computer and use it in GitHub Desktop.
Save huynguyencong/8ffd95a6b4b8afb1b28aca8c6fd71033 to your computer and use it in GitHub Desktop.
A class that helps us to create a custom view from XIB file easier
//
// XibLoaderView.swift
//
//
// Created by Nguyen Cong Huy on 20/11/2021.
//
import UIKit
/**
Usage:
- Create a class, inherite this `XibLoaderView`
- Create a xib file with the same name as the class name.
- Set the class name File Owner field.
- Drag an IBOutlet name `contentView`, then delete it in the code (because it is defined in `LoadableView`).
- That's all for setting up. Just continue designing and coding on .xib and .swift file.
*/
class XibLoaderView: UIView {
@IBOutlet var contentView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit(frame: CGRect? = nil) {
Bundle.main.loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)
addSubview(contentView)
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if let frame = frame {
self.frame = frame
}
setupUI()
}
func setupUI() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment