首页 文章

如何在UIView中加载xib文件

提问于
浏览
102

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

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

6 回答

  • 2

    要以编程方式从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];
    
  • 23

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

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

    Load a Custom UIView from .xib file

  • 18

    你可以尝试:

    UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil] firstObject];
    [self.view.containerView addSubview:firstViewUIView];
    
  • 5

    [快速实施]

    从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))
        }
    }
    
  • 13

    Create a XIB file :

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

    enter image description here

    确保勾选“也创建XIB文件”

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

    你可以选择作为你的申请

    enter image description here

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

    enter image description here

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

    enter image description here

    现在!需要 grab 他们快速文件 . 我很生气 restaurantPhotorestaurantName 你可以帮到你们所有人 .

    enter image description here

    Now adding a UITableView

    enter image description here

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

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

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

    first 如果你没有先定义然后抓取所有视图..所以你需要 grab 那个集合 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
        }
    
    }
    

    你做完了:)

    enter image description here

  • 174

    For swift 3 & 4

    let customView = Bundle.main.loadNibNamed("CustomView", owner: nil, options: nil)?.first as? CustomView
    

相关问题