UIToolbar xib xcode
Asked Answered
M

1

1

I am trying to create a custom UIToolbar that I can add to multiple views, I don't want to do this programmatically so I am using xib to design a UIToolbar. Do I need to wrap this in a UIView and a View Controller? ViewController.m is the root view controller. This is just a simple project I am just trying out some stuff

UIToolbar XIB

Adding CustomToolbar to the Root UIView

Misha answered 2/6, 2014 at 23:57 Comment(1)
did you ever get answer to this?Sporophyte
D
2

You can do this so:

  1. create ToolBar.xib file, delete UIView and add UIToolbar
  2. create ToolBar.swift file, add code, like shown, and make outlets
  3. for .xib in "Identity and Type" put Name "ToolBar.swift"

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfxcF_jZRfRX7ImaFf1KmMva3/Users/Igor/Desktop/Снимок экрана 2016-01-05 в 03.34.33.png

  1. In your root View Controller put this code in 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 put 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")
        }
    
    }
    
Declinatory answered 5/1, 2016 at 1:52 Comment(1)
in 4 point lot of errors. Remove second line (will hide the toolbar), the third line is wrong because it will add the view at top of navigationController, the correct one is: self.navigationController!.toolbar.addSubview(toolBar). The four line is useless.Varnish

© 2022 - 2024 — McMap. All rights reserved.