How to change leftBarbuttonItem title? - objective-c

I want to change leftBarButton title. I've tried this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setLeftBarButtonItem:newBackButton];
but it brings changes like
But I want it like
How it can be possible?

It may helpful for you
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backBarButton;

From the view controller (ViewController1) that is pushing the next view controller (ViewController2), you need to set the backBarButtonItem.
#implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
}

It's totally normal that you don't see any arrow in your back button. That's because you're creating a new bar button that you set its title and its style. But the style you've set for your bar button item won't give you what you wanted.
I suppose that you're using UINavigationController to navigate. If it's the case, you need to get a reference to navigationBar that UINavigationController displays on the screen. Then, you'll be able to change its color via tint color property. So,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
would do what you want to achieve. Here is the image i obtained using the code i mentioned above.
If you're not using UINavigationController, the simplest solution is to provide an image for your back button which has already an arrow.

You should set backBarButtonItem instead of leftBarButtonItem. And remember to set it not in current view controller (where you wish it to be displayed) but in parent one (to which this button will return you).

Use this Code and
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
and for going to back use this method .
-(void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}

This worked for me
UIImage *image = [[UIImage imageNamed:#"btn_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = button;
- (IBAction)backAction:(id)sender
{
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:mainMenuViewController animated:YES];
}

Related

navigation menu not at full width of screen

I'm writing an app in objective C for ios 8, and my navigation bar has an annoying margin thats causing it to be shifted to the right ~20 pixels for leftBarButtonItem and ~20 left for rightBarButtonItem, as shown in the image below. I have tried using negative spacers per some other posts to no avail. Any ideas?
(I'm trying to do this without using storyboards or xibs.)
Here is the code:
UIToolbar* toolbar_temp = [[UIToolbar alloc] init];
toolbar_temp.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45);
toolbar_temp.barTintColor= [UIColor blackColor];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIImage *camimage = [UIImage imageNamed:#"cancel"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:camimage style:UIBarButtonItemStylePlain target:self action:nil];
[items addObject:spaceItem ];
[items addObject:customItem ];
[items addObject:spaceItem ];
[toolbar_temp setItems:items animated:NO];
UIBarButtonItem *allButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar_temp];
self.navigationItem.leftBarButtonItem = allButton;
EDIT:
I'm not sure if this gives any hints, but for some reason this works:
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
While this does not:
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
SOLUTION:
I ended up finding the solution myself. The issue was that I have a tabbar controller which had a navigation controller as one of the views. The navigation controller can only be setup in viewDidload, which doesn't get called in tabbar controllers. The solution was to add viewDidAppear and add the navigation controller within that function.
- (void)viewDidAppear:(BOOL)animated{[self addToolbar];}
Thanks all for the help!
It seems you cannot alter the minimum margins. You can do is add the toolbar_temp to the navigation bar:
[self.navigationController.navigationBar addSubview:toolbar_temp];
Or add to the self.view:
self.navigationController.navigationBarHidden = YES;
[self.view addSubview:toolbar_temp];

How to add more than one UIBarButtonItem to rightBarButtonItem in ios7?

I have an existing code which was working fine in ios6. but in ios7 left most item("refresh button") is not showing align to other two UIBarButtonItem . its showing little down. here is the code for iOS6. what changes do i need to make this working in iOS7.
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshButtonClicked:)];
refreshButton.style=UIBarButtonItemStyleBordered;
//self.navigationItem.rightBarButtonItem = refreshButton;
[buttons addObject:refreshButton];
[refreshButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:self action:#selector(InfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:infoItem];
// 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];
Thanks
Try:
self.navigationItem.rightBarButtonItems = buttons;

Adding leftBarButtonItem programmatically to the app

I am trying to add leftBarButtonItem to my iPhone app. I have Navigation controller and inside it TableViewController.
I am coding the following line in my AppDelegate.m file.
self.viewCon = [[UITableViewController alloc]init];
self.navCon = [[UINavigationController alloc]initWithRootViewController:self.viewCon];
self.viewCon.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
The navigation item does not appear on the screen. The table is on. What might be the prob?
By the way, no ARC or storyboard.
I'm assuming you want a TableView with a NavigationController on top of it, in which case your root viewcontroller would be your navigation controller and not your tableview. If you built the app in IB properly (stab in the dark since I can't see what you did), then you should not have to initialize your navigation controller. If this is the case then it should be as simple as
self.viewCon.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
What I think is happening is your are overwriting your currently set up NavController with your programmatic initialization which is causing everything to disappear.
If all this is the case and you may have tried this, post back the error and a screen shot of your IB setup.
Try this instead:
UIBarButtonItem *bbi = [[[UIBarButtonItem alloc] initWithTitle:#"Second View" style:UIBarButtonItemStyleBordered target:self action:#selector(push)]autorelease];
[self.navigationItem setLeftBarButtonItem:bbi];
[self.navigationController setNavigationBarHidden:NO];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 49,32)];
[ backButton setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = back;
[back release];

Odd bug with UISplitView - Viewport scales to iPhone size?

I'm working with Objective-C for the first time, and have an odd bug in an application that was given to me. The following pictures demonstrate the problem I'm having:
This screen displays when the user rotates the iPad. This is normal. The control at the top (<) allows the user to hide the subview on the left side. Upon hiding the subview, the dark blue portion should expand to fill the screen. Prior to my working with the application, this worked. Now, this is happening:
I haven't found anything to explain why, exactly, this is happening. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[self addGestureRecognizers];
// initialize the "<" button...
toggleButton = [[UIBarButtonItem alloc] initWithTitle:[PanoUtils leftIndicator] style:UIBarButtonItemStylePlain target:self action:#selector(toggleScreen)];
// the annotate button...
annotateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Pencil.png"] style:UIBarButtonItemStylePlain target:self action:#selector(annotateTapped)];
// the done button for annotations...
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneAnnotateTapped)];
// a flexible spacer...
nameFieldSpacerButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// the accelerometer button...
alignButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gyro.png"] style:UIBarButtonItemStylePlain target:self action:#selector(align:)];
// and the search button.
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Search.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTouched:)];
// customize the align button after the user selects it.
UIImage *buttonImage = [UIImage imageNamed:#"gyroSelected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(align:) forControlEvents:UIControlEventTouchUpInside];
alignSelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *itemsArray = [toolBar.items mutableCopy];
[itemsArray addObject:searchButton];
[itemsArray addObject:annotateButton];
[itemsArray addObject:alignButton];
[toolBar setItems:itemsArray animated:NO];
[itemsArray release];
[self adjustToggleButton];
}

ios) Object-c. how to customize a button on the navigation bar

I'm developing a navigation-base application with a custom navigation bar. I succeeded to have a navigation bar with an image and a button. However I have no idea how to make the custom button..:(
This is the code that adds the "main"button on the navigation bar.(in the view controller, initWithNibName method)
if (self) {
// Custom initialization.
//set navigation id to I to inform that that page is inforview
navID = #"I";
//adds the button on the navigation bar
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Main" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack:)];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[navID release];
}
Thank you
I created a custom button and applied it to the UIbarbuttonitem.
There is no error but nothing is shown on the navigation bar:(
This is my code-
//create a custom button
UIImage *image = [UIImage imageNamed:#"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
Anybody who can fix my code? :)
The easiest way is to use:
UIButton *yourCustomButton = ....
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomButton];
self.navigationItem.leftBarButtonItem = barButtonItem;
[barButtonItem release];