Swift development Tutorial: Using Generics to facilitate the loading process of xib, swiftxib

Source: Internet
Author: User

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 )}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.