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?
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 .
Select the UIBarButtonItem in Interface Builder , open the inspector (Command + Shift + I) , and select "Custom" under the dropdown next to Identifier .
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 ...
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.
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
.
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With