Skip to content

Instantly share code, notes, and snippets.

@mnaruse
Last active June 14, 2020 08:06
Show Gist options
  • Save mnaruse/889a7b200ee78c2c2a985250d7f86318 to your computer and use it in GitHub Desktop.
Save mnaruse/889a7b200ee78c2c2a985250d7f86318 to your computer and use it in GitHub Desktop.
Creating a custom UIView from a XIB.
//
// XibView.swift
//
// Created by MiharuNaruse on 2020/06/14.
// Copyright © 2020 m_rn. All rights reserved.
//
// https://medium.com/better-programming/swift-3-creating-a-custom-view-from-a-xib-ecdfe5b3a960
//
import UIKit
class XibView: UIView {
var contentView: UIView!
// コードから初期化
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
// IBから初期化
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
// 初期化するものを書く
// 現在実行中のコードを含むバンドルディレクトリを表す、メインバンドルからnibファイルをロードする。
Bundle.main.loadNibNamed("XibView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment