setBackButtonBackgroundImage without title IOS5 - objective-c

I'm trying to get a back button without a title but I can't make it work. I am really new in objective-c...
UIImage *backButtonImage = [[UIImage imageNamed:#"back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 30, 50)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
With that code I have my back button but also the title of the previous page.
I found some working examples using the class UIViewController but in my case the code is in the appDelegate.m file.
Any idea how I can make it work ?

A very easy hack that I adapted which uses the iOS 5 appearance proxy is the following (needless to say, the benefit of the proxy is global change to all nav bars):
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
forBarMetrics:UIBarMetricsDefault];
This method is available in iOS 5 SDK, so no worries there.
EDIT
In order to avoid the image stretching, use the following:
UIImage* image = [UIImage imageNamed:#"back"];
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)];

From what I can tell, you're setting the appearance proxy correctly. The issue here is setting a new title for your back button.
To do so, create a custom button with your behaviour, and use that as your new back button. Set this before you push or pop said view controller, such as in init
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[self.navigationItem setBackBarButtonItem: newBackButton];

UIImage *image1 = [UIImage imageNamed:#"image.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Above code use for set bacground image in Back button.

as i understand, you are asking how to change the back button's title.
here is how i always do it
self.navigationItem.backBarButtonItem.title=#"My Title";
i guess you can set it as an empty by not giving any title
self.navigationItem.backBarButtonItem.title=#"";
make sure to call this function before you push the new view.

i have not tried it, but i think it should work....Put this into you viewdidLoad, i hope its what you are looking for.
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];

Just add this line after the code you have already written :
[[UIBarButtonItem appearance] setTitle:#""];
Hope it works.

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 change leftBarbuttonItem title?

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];
}

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];

spacing rightBarButtonItems in UINavigationBar

i am using the rightBarButtonItems property of UINavigationBar to add two buttons on the right part of my navigation bar. is it possible to make the spacing between these two buttons wider?
thanks
You can add an UIBarButtonSystemItemFlexibleSpace item between your two buttons.
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
I was not able to get the flexible space to work in my situation but here is the code I used to be able to position the rightBarButtonItem: Note, I put a border around the UIView so you can see what it looks like with having the image in there.
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(89,40,100,30)];
containerView.layer.borderColor = [[UIColor redColor] CGColor];
containerView.layer.borderWidth = 1.0;
UIImage *image = [UIImage imageNamed:#"nav-icon.png"];
UIButton *navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton setFrame:CGRectMake(67,0,25,25)];
[navigationButton setImage:image forState:UIControlStateNormal];
[containerView addSubview:navigationButton];
UIBarButtonItem *navigationBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:containerView];
self.navigationItem.rightBarButtonItem = navigationBarButtonItem;

UINavigationBar setBackgroundImage: forBarMetrics: Not Working

I just switched over to iOS 5 and everything appears to be working in my application aside from the custom navigation bar. I looked around and followed everybody's suggestion of calling the new methods setBackgroundImage: forBarMetrics: however it doesn't appear to work. This is the code I've tried to place both within the app delegate and within the viewDidLoad method of some of the view controllers:
UINavigationBar *nb = [[UINavigationBar alloc]init];
if( [nb respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] )
{
UIImage *image = [UIImage imageNamed:#"navBarBackground.png"];
[nb setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
[nb release];
Unfortunately this doesn't work. If anybody has any suggestions at all, I'm all ears!
To apply image to all your navigation bars, use the appearance proxy:
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
For an individual bar:
// Assuming "self" is a view controller pushed on to a UINavigationController stack
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
In your example, the background image won't change because nb isn't hooked up to anything.
Answer by rob is correct but if application runs on iOS 4.3 or lower app will crash. So you can implement this like
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar-Landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone];
}
This will set image for both mode Landscape and Portrait
You need to get the navigation bar from a navigation controller. Right now you are just creating one and then it deallocs when you release it. You need to get the navigation controller for your view controller.
UINavigationController * navigationController = [self navigationController];
UINavigationBar * nb = [navigationController navigationBar];
Note that if you want to apply the same to a toolbar, it is slightly different. Here's an example, building on Rob's answer:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBarDarkWoodGrain.png"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
You didn't do something like use a UINavigationBar subclass? If you do this and override drawRect: on iOS5 and above, things will break.
See my answer here for a way to support both OS versions.
This worked for me.
UIView *parentViewLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
parentViewLeft.backgroundColor = [UIColor clearColor];
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbar.png"]
forBarMetrics:UIBarMetricsDefault];
} else {
UIImageView* imgBG = [[UIImageView alloc]init];
imgBG.image = [UIImage imageNamed:#"navbar.png"];
imgBG.frame = CGRectMake(-10,0, 330, 44);
[parentViewLeft addSubview:imgBG];
}
UIBarButtonItem *customBarButtomLeft = [[UIBarButtonItem alloc] initWithCustomView:parentViewLeft];
self.navigationItem.leftBarButtonItem = customBarButtomLeft;