簡體   English   中英

如何在UIView中加載xib文件

[英]How to load a xib file in a UIView

我到處都在搜索,到目前為止沒有什么對我有用。

基本上我想要一個名為rootView.xib的.xib文件,在其中我希望有一個UIView(讓我們稱之為containerView)只占用屏幕的一半(因此會有常規視圖和新視圖)。 然后我想要一個名為firstView.xib的不同.xib文件並將其加載到containerView中。 所以我可以在firstView.xib上有一堆東西和rootView.xib中的一堆不同的東西,並在rootView.xib中加載我的firstView.xib在containerView內,但由於它只占用了屏幕的一半,你仍然可以看到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上創建了一個示例項目,用於從另一個.xib文件中的.xib文件加載UIView。 或者你可以通過編程方式完成。

這對於您希望在不同的UIViewController對象上重用的小部件非常有用。

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

從.xib文件加載自定義UIView

你可以嘗試:

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

[快速實施]

從xib加載視圖的通用方法:

例:

let myView = Bundle.loadView(fromNib: "MyView", withType: MyView.self)

執行:

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

創建一個XIB文件:

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

在此輸入圖像描述

確保勾選“也創建XIB文件”

我想用tableview執行所以我選擇了子類UITableViewCell

你可以選擇作為你的申請

在此輸入圖像描述

XIB文件設計符合您的願望(RestaurantTableViewCell.xib)

在此輸入圖像描述

我們需要抓住行高來設置每一行的表格

在此輸入圖像描述

現在! 需要抓住他們快速文件。 我很喜歡這里的restaurantPhotorestaurantName你可以幫到你們所有人。

在此輸入圖像描述

現在添加一個UITableView

在此輸入圖像描述

名稱
nib文件的名稱,不需要包含.nib擴展名。

所有者
要指定為nib的File的Owner對象的對象。

選項
包含打開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
    }

}

你做完了:)

在此輸入圖像描述

對於快速3和4

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

對於Swift 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
    }

}

創建一個類的實例,並在您的視圖中添加您想要的特定布局

let myView = NibView.getScreen()
self.yourView.addSubview(myView)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM