Swift development Tutorial: Using Generics to facilitate the loading process of xib, swiftxib
Swift development tutorials facilitate the xib loading process with generics.
/// UIView + Extension. swift // BasicSwift /// Created by CoderZYWang on. // Copyright©2017 hc. All rights reserved. // import UIKit // load the xibfunc loadNib of the UIView type
(_: T. type)-> T where T: UIView {return Bundle. main. loadNibNamed ("\ (T. self) ", owner: nil, options: nil )?. First! T} // load the UIView type xib (and set the frame parameter) 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. // 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 )}}