How can I add UIBarButtonItem to my navigation controller programmatically? - ios

I want to add UIBarButtonItem to my navigation controller programmatically in both left and right side and change both of them to other UIBarButtonItems when clicked. And also I want to use my item icons. Can i do that? Its my 2nd week in iOS programming excuse me if its a simple question

With image:
self.navigationItem.setLeftBarButtonItem( (UIBarButtonItem(image:UIImage(named:"image"), style: .Plain, target:self, action:"action:")), animated: false)
With title:
self.navigationItem.setLeftBarButtonItem( (UIBarButtonItem(title:"backButton", style: .Plain, target:self, action:"back_pressed:")), animated: false)
Furthermore, you can assign multiple items for each side:
var test: UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"test"), style: UIBarButtonItemStyle.Plain, target: self, action: "test1:")
var test2: UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"test2"), style: UIBarButtonItemStyle.Plain, target: self, action: "test2:")
navigationItem.rightBarButtonItems = [test1, test2]
When using multiple items you might be interested in:
var spacing : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
which is used to have similar space between buttons.

Objective-C version
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"close"] style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
self.navigationItem.leftBarButtonItem = closeButton;
//OR
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
self.navigationItem.leftBarButtonItem = closeButton;

try this code-
self.title = "Title"
var left : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
var right : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")
self.navigationItem.leftBarButtonItem = left
self.navigationItem.rightBarButtonItem = right

Related

How can I add title and image on a bar buttonitem for a toolbar of navigation controller

How it is possible to add an image and text on a UIBarButtonItem on a navigation controller just like on modern apps such as Photos, ebay etc (see Toolbar)
I know this is possible in Interface Builder if you add a toolbar manually to a view but this is not possible if using navigation controller.
If you have a UIViewController in a UINavigationController you can set the left and the right bar button of the view controller with
// System item
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(action))
// Tittle
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: #selector(action))
// Image
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(action))
// Custom view
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: view)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
This is as far as I got so far. This, however only displays a text right to the image:
//Create a UIButton with an image on the left, and text to the right
var buttonImage = #imageLiteral(resourceName: "Lab")
var button : UIButton = UIButton(type: UIButtonType.custom)
button.setImage(buttonImage, for: UIControlState.normal);
button.setTitle("Caption", for: UIControlState.normal);

Hide back button in tabbar navigation controller

I need to hide the Back that is overlapping with Cart
Edit 1
I have already added these things
override func viewWillAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tabBarController!.navigationItem.title = "Orders"
// self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.hidesBackButton = true
}
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController!.navigationItem.title = "Orders"
self.navigationItem.title = "Order History"
// self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
// Do any additional setup after loading the view.
self.navigationItem.hidesBackButton = true
}
in this place
self.navigationItem.hidesBackButton = true
try this
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIView())
option-2
self.tabbarcontroller.navigationcontroller.navigationitem.hidesBackButton = true
self.tabBarController!.navigationItem.rightBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.tabBarController!.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"Cart", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navigationItem.hidesBackButton = YES;
Try with below code, write this code on view controller which appear after 1st navigationcontroller ( PushViewController ).
self.navigationItem.setHidesBackButton(true, animated: false)
self.tabBarController!.navigationItem.hidesBackButton = true

How to add right button in the navigation bar?

I have a question to add a right button in navigation bar..
I have two views: View A and View B
I add a navigation bar to view A, after I used self.navigationController!.pushViewController to show view B.
That show a back button in the left of navigation bar of view B automatic, it is good. but now I want to add a button in the right of navigation bar of view B..
I have tried some answers, but it doesn't work...
I have tried answers likes :
1) https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar
2)http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr
Could you help me, thank you !
The Swift version of Vahan Babayan's answer, as you seem to use this language, is:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: "rightButtonAction:"
)
self.navigationItem.rightBarButtonItem = rightButtonItem
The following method will be called on self:
func rightButtonAction(sender: UIBarButtonItem)
Note that all this can be set graphically using a Storyboard, by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.
A small update since Swift 3 and 4 are out: the compiler can now check selector names, preventing typos when setting up target-action programatically. So one should really use:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: #selector(rightButtonAction(sender:))
)
You can add the following code to your B controller's viewDidLoad method.
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleDone
target:self
action:#selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:#"Right Button" style:UIBarButtonItemStyleDone target:self action:#selector(rightBtnClick)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)rightBtnClick{
// code for right Button
}
Add below code in viewDidLoad Method
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
This adds a button to the right hand side with the title Flip, which calls the method:
-(IBAction)flipView
Swift code to add a navigation bar button item:
Method 1 (When you wanna use bar button system item)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
Method 2 (When you wanna give your own title to button)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar. Something like this:
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
navigationItem.rightBarButtonItems = [add, display]
Try this code for Swift UI :
.navigationBarItems(
leading:
HStack {
Button("About") {
print("About tapped!")
}
Button("Call") {
print("Call tapped!")
}
},
trailing:
HStack {
Button("Settings") {
print("Settings tapped!")
}
Button("Contacts") {
print("Contacts tapped!")
}
}
)

Add a left bar button item to UINavigationController when no back button is present

I'd like to add a default left bar button item to my navigation bar. It should only display when there is no back button supplied by the UINavigationController.
What is the best approach?
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
return;
}
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:#selector(menuItemSelected:)];
[viewController.navigationItem setLeftBarButtonItem:menuItem];
}
Swift code to add leftBarButtonItem…
let leftMenuItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "leftMenuItemSelected:");
self.navigationItem.setLeftBarButtonItem(leftMenuItem, animated: false);
Update
For Swift 4 & above
let leftMenuItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(leftMenuItemSelected(_:)))
self.navigationItem.setLeftBarButton(leftMenuItem, animated: false);
Swift 3 version:
let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(YourController.done))
navigationItem.setLeftBarButtonItem(done, animated: false)
For Swift 4.x and later(5.x)
func addNavigationItemBtn(){
let backBtn = UIBarButtonItem.init(title: "Menu", style: .plain, target: self, action: #selector(menuAction))
self.navigationItem.leftBarButtonItem = backBtn
}
#objc func menuAction(){
print("menu button pressed") //Do your action here
}
Swift 4
let item = UIBarButtonItem(title: "Setting", style: .done, target: self, action: #selector(setting))
navigationItem.rightBarButtonItem = item
// then do your action in function setting
#objc
final func setting() {
print("Your work here.")
}

Aligning UIToolBar items

I have three UIBarButtonItem created as below. They align left and I'd like to align center so there isn't a gap on the right side. I don't see an align property on UIToolBar. Is there another way to accomplish this?
//create some buttons
UIBarButtonItem *aboutButton = [[UIBarButtonItem alloc] initWithTitle:#"About" style:UIBarButtonItemStyleBordered target:self action:#selector(showAbout:)];
[toolbar setItems:[NSArray arrayWithObjects:settingsButton,deleteButton,aboutButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
Add two UIBarButtonSystemItemFlexibleSpace items to your toolbar, to the left and right of your items
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, settingsButton,deleteButton,aboutButton, flexibleSpace, nil]];
Adding these as you would any other toolbar items will distribute space evenly between the two of them.
This can also be done right from a storyboard.
Just drag and drop items in the toolbar, and turn some of them into flexible or fixed space to get the desired effect. See the two examples below.
In Xamarin iOS
Right aligned:
yourBar.SetItems(new [] { new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), yourButton }, false);
Center Aligned:
var flexibleSpace = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
yourBar.SetItems(new [] { flexibleSpace, yourButton, flexibleSpace}, false);
Swift version:
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: viewController.view.frame.size.width, height: 35.0))
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: viewController, action: nil)
let button1 = UIBarButtonItem(title: "A", style: UIBarButtonItem.Style.Plain, target: viewController, action: foo)
let button2 = UIBarButtonItem(title: "B", style: UIBarButtonItem.Style.Plain, target: viewController, action: bar)
let button3 = UIBarButtonItem(title: "C", style: UIBarButtonItem.Style.Plain, target: viewController, action: blah)
toolbar.items = [button1, flexibleSpace, button2, flexibleSpace, button3]

Resources