Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding UIBarButton item in swift

I'm trying to add a UIBarButton item to my nav bar.

Here is my Navigation Bar class declaration:

import UIKit

class NavigationBarController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        configureToolbar()        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // #pragma mark - Navigation bar data source

    func configureToolbar() {
        let toolbarButtonItems = [
            searchBarButtonItem
        ]
        toolbar.setItems(toolbarButtonItems, animated: true)
    }

    var searchBarButtonItem: UIBarButtonItem {
        return UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:")
    }
}

No error in compiler, but all I get is a plain navigation bar.

How do I get the UIBarButtonItem to show up?

like image 823
azochz Avatar asked Jul 18 '14 00:07

azochz


People also ask

How do I add a navigation bar button in Swift?

You need to open the storyboard, delete the view controller that you have, press cmd , shift , l , and then search for navigation controller . Drag that onto the storyboard. You now need to click on the navigation controller and set it to be the is initial view controller under the attributes inspector .

How do I change my UIBarButtonItem text?

Select the UIBarButtonItem in Interface Builder , open the inspector (Command + Shift + I) , and select "Custom" under the dropdown next to Identifier .

How do I hide UIBarButtonItem?

UIBarButtonItem doesn't have a hidden property, and any examples I've found so far for hiding them involve setting nav bar buttons to nil, which I don't think I want to do here because I may need to show the button again (not to mention that, if I connect my button to an IBOutlet, if I set that to nil I'm not sure how ...

How do I change the size of my UIBarButtonItem?

You can set the font size for a specific BarButtonItem by overriding its ContentTemplate. Put an AccessText to this template and set its FontSize property. If you want to change the font size for all items in a specific BarManager, use the attached TextBlock. FontSize property.


2 Answers

To add items to the NavigationBar of a NavigationController, or a NavigationBar added to a ViewController, you will need to first go through NavigationItem. Try this:

self.navigationItem.setRightBarButtonItems(navigationBarButtonItemsArray, animated: true)

// Or if you just want to insert one item.

self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)

To switch the button to the left side, just replace setRightBarButtonItem to setLeftBarButtonItem or setLeftBarButtonItems.

like image 96
hichamoaj Avatar answered Sep 18 '22 22:09

hichamoaj


You can add multiple buttons to right side or left side of the navigation bar. I wil show u to add on right side and you can do the same for left side too

override func viewDidLoad()
{
    let Nam1BarBtnVar = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: #selector(Nam1BarBtnKlkFnc(_:)))
    let Nam2BarBtnVar = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: #selector(Nam2BarBtnKlkFnc(_:)))

    self.navigationItem.setRightBarButtonItems([Nam1BarBtnVar, Nam2BarBtnVar], animated: true)
}

func Nam1BarBtnKlkFnc(BtnPsgVar: UIBarButtonItem)
{
    print("Nam1BarBtnKlk")
}

func Nam2BarBtnKlkFnc(BtnPsgVar: UIBarButtonItem)
{
    print("Nam2BarBtnKlk")
}
like image 44
Sujay U N Avatar answered Sep 17 '22 22:09

Sujay U N