Swift 開發教程之利用 泛型 便利 xib 的載入過程,swiftxib
Swift 開發教程之利用 泛型 便利 xib 的載入過程。
//// UIView+Extension.swift// BasicSwift//// Created by CoderZYWang on 2017/11/28.// Copyright © 2017年 hc. All rights reserved.//import UIKit/// 載入 UIView 類型的 xibfunc loadNib(_ : T.Type) -> T where T: UIView { return Bundle.main.loadNibNamed("\(T.self)", owner: nil, options: nil)?.first as! T}/// 載入 UIView 類型的 xib(並設定 frame 參數)func loadNib(_ : T.Type, frame: CGRect) -> T where T: UIView { let TView = loadNib(viewType) TView.frame = frame return TView}
//// HomeViewController.swift// BasicSwift//// Created by CoderZYWang on 2017/11/27.// Copyright © 2017年 hc. All rights reserved.//import UIKitclass HomeViewController: UIViewController { override func viewDidLoad() { setUpUI() }}extension HomeViewController { func setUpUI() { view.backgroundColor = UIColor(hex: "FF5A39", alpha: 0.5) let laolaoView = loadNib(LaoLaoView.self, frame: CGRect(x: 20, y: 220, width: 300, height: 150)) view.addSubview(laolaoView) let laolaoView02 = loadNib(LaoLaoView.self) view.addSubview(laolaoView02) laolaoView02.frame = CGRect(x: 20, y: 400, width: 200, height: 120) }}