Actually i am placing a bar button on right side of navigation bar, it's not working. But when i use it as left bar button item, it's working fine. I am using ios5.
It was also not working when i have both button left and right bar button. Then i set the frame for both, then these are working. But when i have only one button on right side, it's not working.
UIButton *but1 = [UIButton buttonWithType:UIButtonTypeCustom];//customising map button.
but1.frame = CGRectMake(270,0,50,40);
[but1 addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];//on cilcking an map button clicked method is called.
buttonRight = [[UIBarButtonItem alloc]initWithCustomView:but1];//setting map button on Navigation bar.
self.navigationItem.rightBarButtonItem = buttonRight;//setting button on the Right of navigation bar.
how to trace out this error?
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle: #"edit" style: UIBarButtonItemStyleBordered target: self action: #selector(edit_details)];
self.navigationItem.rightBarButtonItem = addButton;
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 50, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:0];
// create a standard BarButtonItem
UIBarButtonItem *SettingsBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"icon_setting.png"] style:UIBarButtonItemStylePlain
target:self
action:#selector(ActionMethod:)];
[buttons addObject:SettingsBtn];
[SettingsBtn release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
[toolbar release];
For Swift 4.2 code:
In the View did load function
let addButton = UIBarButtonItem(title: "edit", style: .plain, target: self, action: #selector(edit))
navigationItem.rightBarButtonItem = addButton
Function definition goes here:
#objc func edit() {
// Body definition
}
Related
I can implement navigation bar and add left bar button, but before left button have space, please ask how to remove this space in ios 7.
use this it will work i have used it and works fine on iOS 7 also
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithCustomView:segmentView];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -6;// it was -6 in iOS 6 you can set this as per your preference
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,homeButton, nil] animated:NO];
You could use a system .fixedSpace UIBarButtonItem.
Here is a swift version producing the expected result (iOS 10+):
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -20 // working on iOS 10.
let barButton = UIBarButtonItem(title: "<# title #>", style: .plain, target: <# target #> , action: #selector(<# action #>))
navigationItem.leftBarButtonItems = [negativeSpacer, barButton]
Another idea is to add a UIButton to the UINavigationController.view directly.
We can add customize left bar button of navigation bar, but before left button have space. I fix this issue as following,
Calling:
// MyViewController.m
- (void)viewDidLoad {
UIBarButtonItem *bbBack = [Utilities overrideBackBarButtonItemWithTarget:self action:#selector(btnBackTapped:)];
}
- (void)btnBackTapped:(id)sender {
//Custom navigation back bar button item
}
Add this class method to your Utilities class or any common class for your project,
Implementation:
// Utilities.h
#interface Utilities : NSObject
#pragma mark - back bar button item
+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action;
#end
// Utilities.m
#implementation Utilities
+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action {
// back button
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65 , 44)];
[btn setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);//move image to the right
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbuttonBack = [[UIBarButtonItem alloc] initWithCustomView:btn];
//navigation spacer
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spacer.width = -15;// it was -6 in iOS 6 you can set this as per your preference
//set leftBarButtonItems
UIViewController *_self = (UIViewController*)target;
[_self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:spacer,barbuttonBack, nil] animated:NO];
return barbuttonBack;
}
#end
Back image:
back.png
this is a simple problem, but i'm new to xcode dev. I followed a guide online to have multiple buttons on a navigation bar. the edit button on the navgivation bar has a IBAction method called "editButton". which has a (id)sender as parameter. do I get the sender and change the text from edit to done, done to edit?
"UIBarButtonitem *bbi = (UIBarButonItem *) sender;" doesn't seem to be working. how do i get the button in the toolbar in navigationbar?
Thank you.
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "EDIT" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButton:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
-(IBAction)editButton:(id) sender{
UIBarButtonitem *bbi = (UIBarButonItem *) sender;
if (bbi title isequalsString:#"Done){
[bbi setTitle:#"Edit"];
}
}else{
[bbi setTitle:#"Done"];
}
}
The trouble is that you have used a UIBarButtonSystemItemEdit, not a standard uibarbutton.
Try creating it with:
bi = [UIBarButtonItem alloc] initWithTitle:#"Edit" style: UIBarButtonItemStyleBordered target:self action:#selector(editButton:)];
Then use the rest of the code as is.
UIBarButtonSystemItemEdit is a special bar button item that takes care of the "Edit / Done" states for you. No need to manually change the button's text.
how can i add buttons just beside a left barButton or just beside rightbarButton,
i mean i need to have more than 2 buttons on the title bar is that possible?
thanx in advance
You can use a view with two buttons in your leftBarButton -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
As drawn from this blog post:
Think of a toolbar as your container,
instead of the UIView object. And
since a UIToolBar is UIView based, it
can be added to the right side of a
nav bar using the above trick. The
following code shows how you can add
two standard buttons to the right
side:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
Also check the SO post
Adding buttons to navigation bar
I would suggest to use UISegmentedControl to hold all your buttons and use
[[UIButton alloc] initWithCustomView:] to show the segment control.
Here is a sample of how you could go about doing this. I am using a MasterDetailView template that comes with the top navigation bar for this example:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = #"Left button";
rightButton.title = #"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
You can do something like this, there's something called UINavigationItem.rightBarButtonItems
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"] style:UIBarButtonItemStyleDone target:self action:#selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"]
style:UIBarButtonItemStyleDone target:self
action:#selector(something)];
self.navigationItem.rightBarButtonItems = #[settingsButton, menuButton];
i think you need something like this
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=#[firstButton, secondButton];
it will add two bar button items on left of navigation bar. you can make it on right side the same way
self.navigationController.navigationItem.rightBarButtonItems=#[firstButton, secondButton];
I have a typical UINavigationController scheme but I have many many views which can mean you can end up having quite a few views stacked on top of each other. I want to provide a home button and originally I was going to put that on the right hand side of the navigation bar; however, I have a search bar there and another button so I am hoping to put it next to the back button.
In ascii art:
< Back | |Home| Title |Browse| [Search Bar]
Now I tried to set the backButtonItem of the previous view controller using the following:
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
back.title = #"Back";
// create a standard save button
[buttons addObject:back];
[back release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home32.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onHomeButton:)];
homeButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:homeButton];
[homeButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
but that doesn't seem to replace the standard back button. If I just try one UIBarButtonItem and set the backButton, that works.
Now the other approach I tried is that on the View that is pushed on the stack, to set the leftBarButtonItem but I can't seem to find a way to create a back button that has the pointy shape to the left.
Any suggestions?
can you try
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]
initWithCustomView:toolbar];
good luck
I have a UIViewController with a UITableView in it, and also added a UINavigationBar.
How can I add and "edit" button and a "+" button in that bar programmatically?
(I have tried using IB, but the title is always replaced, and not other items are added)
I am not using a UINavigationController. is my UIViewController standing alone.
This is what I have tried without success:
UIBarButtonItem *barButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:nil
action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];
Your UIViewController has a navigationItem property. You can set the left and right bar button items with self.navigationItem.leftBarButtonItem = ... and self.navigationItem.rightBarButtonItem = ...
Edit:
OK, I assume you have a reference to your UINavigationBar?
Then I guess you'd add a single UINavigationItem to it:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable
and then set that item's left and right buttons:
theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;
I haven't tried this, but I think it should work.
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(theEditMethod:)];
[viewController.navigationItem setLeftBarButtonItem:leftBarButton animated:NO];
[leftBarButton release];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(theAddMethod:)];
[viewController.navigationItem setLeftBarButtonItem:rightBarButton animated:NO];
[rightBarButton release];
Yo do not need to add UINavigationItem to the UINavigationBar. Yo can do as this example:
NSString *backButtonTittle=[NSString stringWithFormat:#"< %#",NSLocalizedString(#"backButton", nil)];
UIBarButtonItem *backCreateAccountNavBarItem=[[UIBarButtonItem alloc]initWithTitle:backButtonTittle style:UIBarButtonItemStylePlain target:self action:#selector(goToBackStep)];
self.createAccountNavBar.topItem.leftBarButtonItem=backCreateAccountNavBarItem;
Just use your viewController's navigationItem property.
like this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(updateContent)];
Navigation Bar items/buttons with actions on Swift 4.2, ios 11, XCode 10
1) With Storyboard go to Editor > Embed In > Navigation Bar
2) In AppDelegate > didFinishLaunchingWithOptions:
UINavigationBar.appearance().barTintColor = UIColor(hexString: "1C9B90")
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
3) On viewDidLoad for your viewcontroller, create buttons and add to Navigation Bar:
self.navigationController?.navigationBar.topItem?.title = "Title"
self.navigationController?.isNavigationBarHidden = false
//QR Code button
let qrCodeScanButton = UIButton(type: .custom)
qrCodeScanButton.setImage(UIImage(named: "camera"), for: .normal)
qrCodeScanButton.addTarget(self, action: #selector(self.searchWithQRCode), for: .touchUpInside)
let qrCodeScanButtonItem = UIBarButtonItem(customView: qrCodeScanButton)
///LogOut button
let logoutButton = UIButton(type: .custom)
logoutButton.setImage(UIImage(named: "logOut"), for: .normal)
logoutButton.addTarget(self, action: #selector(self.logOut), for: .touchUpInside)
let logoutButtonItem = UIBarButtonItem(customView: logoutButton)
self.navigationController?.navigationBar.topItem?.setRightBarButtonItems([logoutButtonItem, qrCodeScanButtonItem], animated: true)
4) Actions for buttons:
#objc func logout(){
///present your Login VC
}
#objc func qrCodeScanButton(){
///present your Login VC
}
5) Build and Run
PS. Keep in mind these two differences.
isNavigationBarHidden: A Boolean value that indicates whether the navigation bar is hidden.
self.navigationController?.isNavigationBarHidden
navigationBar.isHidden: The navigation bar managed by the navigation controller.
self.navigationController?.navigationBar.isHidden