如何在UIView中加载xib文件

Mat*_*att 109 objective-c xib uiview ios

我到处都在搜索,到目前为止没有什么对我有用.

基本上我想要一个名为rootView.xib的.xib文件,在其中我希望有一个UIView(让我们称之为containerView)只占用屏幕的一半(因此会有常规视图和新视图).然后我想要一个名为firstView.xib的不同.xib文件并将其加载到containerView中.所以我可以在firstView.xib上有一堆东西和rootView.xib中的一堆不同的东西,并在rootView.xib中加载我的firstView.xib在containerView中,但由于它只占用了屏幕的一半,你仍然可以看到rootView.xib上的东西

cho*_*own 179

要以编程方式从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];
Run Code Online (Sandbox Code Playgroud)


Pau*_*olt 24

我在github上创建了一个示例项目,用于从另一个.xib文件中的.xib文件加载UIView.或者你可以通过编程方式完成.

这对于您希望在不同的UIViewController对象上重用的小部件非常有用.

  1. 新方法:https://github.com/PaulSolt/CustomUIView
  2. 原始方法:https://github.com/PaulSolt/CompositeXib

从.xib文件加载自定义UIView


NJo*_*nes 20

你可以尝试:

UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil] firstObject];
[self.view.containerView addSubview:firstViewUIView];
Run Code Online (Sandbox Code Playgroud)


Мак*_*нов 17

[快速实施]

从xib加载视图的通用方法:

例:

let myView = Bundle.loadView(fromNib: "MyView", withType: MyView.self)
Run Code Online (Sandbox Code Playgroud)

执行:

extension Bundle {

    static func loadView<T>(fromNib name: String, withType type: T.Type) -> T {
        if let view = Bundle.main.loadNibNamed(name, owner: nil, options: nil)?.first as? T {
            return view
        }

        fatalError("Could not load view with type " + String(describing: type))
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 根据最新的 Swift: `let view = Bundle.main.loadNibNamed(name,owner: nil, options: nil)?.first as? T` (2认同)

Naz*_*san 6

创建一个XIB文件:

文件 - >新文件 - > ios-> cocoa touch class - > next

在此输入图像描述

确保勾选"也创建XIB文件"

我想执行tableview所以我选择了子类UITableViewCell

你可以选择作为你的申请

在此输入图像描述

XIB文件设计符合您的愿望(RestaurantTableViewCell.xib)

在此输入图像描述

我们需要抓住行高来设置每一行的表格

在此输入图像描述

现在!需要抓住他们快速文件.我很生气restaurantPhoto,restaurantName你可以帮到你们所有人.

在此输入图像描述

现在添加一个UITableView

在此输入图像描述

name
nib文件的名称,不需要包含.nib扩展名.

owner
要指定为nib的File的Owner对象的对象.

options
包含打开nib文件时要使用的选项的字典.

首先, 如果你没有先定义,然后抓取所有视图..所以你需要抓住该集内的一个视图frist.

Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView
Run Code Online (Sandbox Code Playgroud)

这是表视图控制器的完整代码

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
    }

}
Run Code Online (Sandbox Code Playgroud)

你做完了:)

在此输入图像描述

  • 您的示例可以从xib加载查看.但对于tableView它是错误的 - 可重用的单元格是正确的方式 (6认同)

小智 5

对于斯威夫特 4.2

假设您有一个名为NibView的类,关联的 nib 文件是NibView.xib

class NibView: UIView {

    class func getScreen() -> NibView {
        let xib = Bundle.main.loadNibNamed(String(describing :self), owner: self, options: nil)
        let me = xib![0] as! NibView
        return me
    }

}
Run Code Online (Sandbox Code Playgroud)

创建该类的一个实例,并在您的视图中添加您想要的特定布局

let myView = NibView.getScreen()
self.yourView.addSubview(myView)
Run Code Online (Sandbox Code Playgroud)