如何在UIView中加载一个xib文件
我一直在寻找,到目前为止我没有任何工作。
基本上我想有一个叫做rootView.xib的.xib文件,里面我想要一个UIView(让我们称之为containerView)只占用屏幕的一半(所以会有常规的视图和一个新的视图)。 然后我想要一个名为firstView.xib的不同的.xib文件,并将其加载到containerView中。 所以我可以在firstView.xib和一堆不同的东西在rootView.xib上加载一堆东西,并在rootView.xib中的containerView中加载我的firstView.xib,但由于它只占用屏幕的一半,所以仍然可以看到东西在rootView.xib
要以编程方式从xib文件获取对象,可以使用: [[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil]返回xib中顶级对象的数组。 
所以,你可以做这样的事情:
 UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:@"MyRootView" owner:self options:nil] objectAtIndex:0]; UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:@"MyContainerView" owner:self options:nil] lastObject]; [rootView addSubview:containerView]; [self.view addSubview:rootView]; 
我在github上创build了一个示例项目,以从另一个.xib文件中的.xib文件加载UIView。 或者你可以通过编程来完成。
这对于要在不同的UIViewController对象上重用的小部件很有用。
- 新方法: https : //github.com/PaulSolt/CustomUIView
- 原始方法: https : //github.com/PaulSolt/CompositeXib

你可以尝试:
 UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil] firstObject]; [self.view.containerView addSubview:firstViewUIView]; 
[迅速实施]
从xib加载视图的通用方式:
例:
 let myView = NSBundle.loadView(fromNib: "MyView", withType: MyView.self) 
执行:
 extension NSBundle { static func loadView<T>(fromNib name: String, withType type: T.Type) -> T { if let view = NSBundle.mainBundle().loadNibNamed(name, owner: nil, options: nil)?.first as? T { return view } fatalError("Could not load view with type " + String(type)) } } 
创build一个XIB文件:
File – > new File – > ios-> cocoa touch class – > next
  
 
确保勾选“还创buildXIB文件”
 我想用tableview来执行,所以我select了子类UITableViewCell 
你可以select作为你的要求
  
 
XIB文件devise为您的愿望(RestaurantTableViewCell.xib)
  
 
我们需要抓住行高来设置每一行的表格hegiht
  
 
 现在! 需要把他们迅速的文件。 我打了restaurantPhoto和restaurantName你可以把你所有人。 
  
 
现在添加一个UITableView
  
 
  名称 
  nib文件的名称,不需要包含.nib扩展名。 
  所有者 
 要分配为笔尖的文件所有者对象的对象。 
  选项 
 包含打开nib文件时使用的选项的字典。 
  首先,如果你不先定义然后抓取所有的视图..所以你需要抓住一个视图内的设置frist 。 
 Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView 
这里是表视图控制器完整的代码
 import UIKit class RestaurantTableViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{ override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let restaurantTableviewCell = Bundle.main.loadNibNamed("RestaurantTableViewCell", owner: self, options: nil)?.first as! RestaurantTableViewCell restaurantTableviewCell.restaurantPhoto.image = UIImage(named: "image1") restaurantTableviewCell.restaurantName.text = "KFC Chicken" return restaurantTableviewCell } // set row height func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 150 } } 
你做了:)
 