UIToolbar xib xcode

霸气de小男生 提交于 2019-12-02 04:11:54

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"

  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")
        }
    
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!