2016-12-01 62 views
0

我可以在Xib文件中創建自定義UIToolBar嗎?使用xib自定義UIToolBar [Swift 3]

我想製作一個工具欄,在最左側設置一個完成按鈕,在左側設置最後一個/下一個按鈕。但我想確保約束條件能夠針對每個手機尺寸正確縮放。有沒有例子?

+0

只想添加這個環節,我搞砸了幾次,不得不這樣做是爲了讓一切運行正確。 http://stackoverflow.com/questions/29923881/could-not-insert-new-outlet-connection-could-not-find-any-information-for-the-c – rutheferd

回答

2

是的,你可以。首先選擇一個xib作爲模板。命名模板「工具欄」。 create the xib

然後刪除它給你的默認視圖。轉到右下角的對象庫並拖出一個工具欄。 drag out a toolbar xib

您將在最左側獲得一個默認的酒吧按鈕項目。將其重命名爲「完成」,然後拖出另一個欄按鈕項並將其重命名爲「Next」或「Last」。 rename bar button items

現在,創建一個新文件。選擇Cococa Touch Class並將其設爲UIToolBar的一個子類。 enter image description here

返回工具欄xib,並將其類創建爲您剛創建的Cocoa Touch類。在我的例子中,我命名了我的ToolBarExample。

現在轉到您的ViewController類。在ViewDidLoad中添加以下內容。

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let toolBar = UINib(nibName: "toolbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ToolBarExample 

    toolBar.frame = CGRect(x: 0, y: self.view.frame.size.height - 46, width: self.view.frame.size.width, height: 46) 

    toolBar.sizeToFit() 

    view.addSubview(toolBar) 

} 

您可以通過調整其框架來更改更改工具欄的比例,大小和位置。您還可以從Atrributes和Size Inspector中調整工具欄xib及其欄按鈕項目。該示例的最終產品應該在模擬器中如下所示。

Final Product in Simulator

+0

正是我在找什麼!謝謝! – rutheferd