Swift

カスタムUIViewのサンプル

More than 1 year has passed since last update.

汎用的なUIViewを自作したい時のサンプル

CustomUIViewSample.swift
import UIKit

class CustomUIView: UIView {

    var nibView: UIView!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    func setup() {
        self.nibView = NSBundle.mainBundle().loadNibNamed("???", owner: self, options: nil).first as! UIView
        self.nibView.frame = self.bounds
        self.nibView.translatesAutoresizingMaskIntoConstraints = true
        self.nibView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        self.addSubview(self.nibView)
    }
}