// // UIView+Extension.swift // BasicSwift // // Created by CoderZYWang on 2017/11/28. // Copyright © 2017年 hc. All rights reserved. // import UIKit /// 加载 UIView 类型的 xib func loadNib<T>(_ : 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>(_ : 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 UIKit class 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) } }