UIToolbar xib xcode

I am trying to create a custom UIToolbar that I can add to several views, I do not want to do this programmatically, so I use xib to develop UIToolbar. Do I need to wrap this in a UIView and View Controller? ViewController.m is the root view controller. This is just a simple project that I just tried.

UIToolbar XIB

Adding CustomToolbar to the Root UIView

+1
ios objective-c xcode uitoolbar
Jun 02 '14 at 23:57
source share
1 answer

You can do it like this:

  • create ToolBar.xib file, delete UIView and add UIToolbar
  • create the ToolBar.swift file, add the code as shown, and make exits
  • for .xib in the "Identity and Type" put the name "ToolBar.swift"

/ Users / Igor / Desktop / Screenshot 2016-01-05 at 03.34.33. PNG

  1. In the root view of the View Controller, put this code in the ViewDidLoad:

    let toolBar = UINib(nibName: "ToolBar", bundle: nil).instantiateWithOwner(nil, options: nil).first as! ToolBar toolBar.hidden = true self.navigationController!.view.addSubview(toolBar) self.navigationController!.toolbarItems = toolBar.items 
  2. In all View controllers in ViewDidLoad:

     self.toolbarItems = self.navigationController!.toolbarItems 
  3. ToolBar.swift:

     import UIKit class ToolBar: UIToolbar { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } @IBAction func item1Pressed(sender: AnyObject) { print("item 1 pressed") } @IBAction func item2Pressed(sender: AnyObject) { print("item 2 pressed") } @IBAction func item3Pressed(sender: AnyObject) { print("item 3 pressed") } @IBAction func item4Pressed(sender: AnyObject) { print("item 4 pressed") } } 
+1
Jan 05 '16 at 1:52
source share



All Articles