Custom Back button with image for iOS6+ - ios

I'm trying to have a custom back button with image only (no text).
I'm aware that there could be slightly ifferent solution for iOS7 and iOS6. For now I'm not successfull with any.
All I achieved was this:
White default arrow is still there :(
I used this code:
UIImage *temp = [[UIImage imageNamed:#"button_back.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:temp style:UIBarButtonItemStyleBordered target:self action:#selector(action)];
[[self navigationItem] setBackBarButtonItem:backButton];

You should use leftBarBttonItem for custom back button
UIImage *temp = [[UIImage imageNamed:#"button_back.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:temp style:UIBarButtonItemStyleBordered target:self action:#selector(action)];
self navigationItem.leftBarButtonItem = backButton;

Related

UiNavigationbar buttons not responding after upgrade to iOS 11

After upgrading to iOS 11 and to XCode9 my App's NavigationBar buttons (left and right) are not responding. It is working well in previous version but not in iOS 11. can anybody help me or have faced the same issue. My App is in Objective c language.
**//here is code for leftbarbutton**
UIImage *backSVG = [UIImage imageWithSVGNamed:#"backarrow32" targetSize:CGSizeMake(25, 25) fillColor:[UIColor whiteColor]];
UIBarButtonItem *notifyButton = [[UIBarButtonItem alloc] initWithImage:backSVG style:UIBarButtonItemStylePlain target:self action:#selector(handleBackButton)];
[notifyButton setStyle:UIBarButtonItemStylePlain];
self.navigationItem.leftBarButtonItem = notifyButton;
**//Rightbar buttons are**
UIBarButtonItem *rightbar1 = [[UIBarButtonItem alloc] initWithImage:
[UIImage imageWithSVGNamed:#"rightbar1" targetSize:CGSizeMake(21, 21)
fillColor:[UIColor whiteColor]] style:UIBarButtonItemStylePlain target:self
action:nil];
[rightbar1 setAction:#selector(handlerightbar1Action)];
UIBarButtonItem *rightbar2 = [[UIBarButtonItem alloc] initWithImage:
[UIImage imageWithSVGNamed:#"rightbar2" targetSize:CGSizeMake(21, 21)
fillColor:[UIColor whiteColor]] style:UIBarButtonItemStylePlain
target:self action:nil];
[rightbar2 setAction:#selector(handlerightbar2Action)];
[rightbar1 setImageInsets:UIEdgeInsetsMake(0, -30, 0, -50)];
self.navigationItem.rightBarButtonItems = [NSArray
arrayWithObjects:rightbar2, rightbar1, nil];
Is this navigation bar you added in interface builder or it is from navigation controller (either pushed or root view controller)?
If you can attache an image of the UI view controller from interface builder, it will give more details of the issue.

UIBarButtonItem disappears

Why would my UIBarButtonItem sometimes disappear?
When you first load the screen (the first in a Tab Bar) it displays fine. However when you navigate to a new screen and then return both left and right buttons disappear. They aren't reacting to touch events either. However, if you go to a second tab and return they suddenly appear.
It must be a recent iOS change 'cos it was working previously.
==== UPDATE
The code is hardly rocket science but here it is:
Screen 1:
In viewDidLoad:
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:myImage
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
self.navigationItem.rightBarButtonItem = item;
and
- (IBAction)myAction:(id) sender {
MyController *mc = [[MyController alloc] initWithNibName:#"MyController" bundle:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

Can not show the rightbar button - ios sdk

I had add image at my navigation bar and need to add the right bar refresh button.
But the right bar button can not show at the navigation after add.
Why?
Here the code add image at navigation bar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"topbar.png"] forBarMetrics:UIBarMetricsDefault];
Here the add right bar button at viewDidLoad:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
NSArray *actionButtonItems = #[refreshItem];
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
You should not add to self.navigationcontroller.
Do something like this will resolve your problem.
self.navigationItem.rightBarButtonItem = refreshItem;
Try with following code
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
self.navigationItem.rightBarButtonItem = refreshItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightButtoneClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
Right this code in viewDidLoad and you get the refresh button on right side.
Replace this
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
With:
self.navigationItem.rightBarButtonItems = actionButtonItems;

Any idea why the custom nav bar back button image isn't displaying with this code?

Any idea why the custom nav bar back button image isn't displaying with this code?
instead of showing the uiimage it's showing the "< Back" default iOS button in text only
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"freccia.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:#selector(backBtnPress)];
could it be this parameter? UIBarButtonItemStylePlain
thanks
use this code:
UIImage *imageBack = [UIImage imageNamed:#"yourImage.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.bounds = CGRectMake(0, 0, imageBack.size.width, imageBack.size.height);
[btnBack setBackgroundImage:imageBack forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = barButtonBack;
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
You usually set the back button in the parent view controller. However, it might just be easier to use leftBarButtonItem instead of backBarButtonItem.
you can solve this problem by two ways
1. use leftBarButtonItem instead of backBarButtonItem
2. try the below code
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];

UIBarButtonItems not calling actions

I've created a UIToolBar with UIBarButtonItems but when I tap them, the selector's are not being called. Any help would be greatly appreciated.
UPDATE
Based on suggestions in the comments, I modified my code, but unfortunately, the buttons still do not work.
-(void)buildBottomMenu
{
UIImage *newsImg = [UIImage imageNamed:#"243-globe"];
UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadHome)];
UIImage *weatherImg = [UIImage imageNamed:#"25-weather"];
UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:#selector(loadWeather)];
UIImage *sportsImg = [UIImage imageNamed:#"63-runner"];
UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadSports)];
UIImage *entrnImg = [UIImage imageNamed:#"178-city"];
UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:#selector(loadEntertainment)];
UIImage *videoImg = [UIImage imageNamed:#"69-display"];
UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:#selector(loadVideoPage)];
UIImage *moreImg = [UIImage imageNamed:#"259-list"];
UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:#selector(loadOptionsPage)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
[_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
weatherBtnItem,flexibleSpace,
sportsBtnItem,flexibleSpace,
entrnBtnItem,flexibleSpace,
videoBtnItem,flexibleSpace,
moreBtnItem,flexibleSpace,
nil]];
[self.view addSubview:_bottomBar];
}
Here is a sample selector:
- (void)loadWeather
{
NSLog(#"Loading weather");
}
thanks for the help, but I just realized that it was something I was doing in my viewDidLayoutSubviews method that was causing the problem.
Try this,
-->First drog and drop UIToolbar to your ViewController.xib
-->connect IBOutlet connection
ViewController.h
IBOutlet UIToolbar *_bottomBar;
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self buildBottomMenu];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)buildBottomMenu
{
UIImage *newsImg = [UIImage imageNamed:#"243-globe"];
UIBarButtonItem *newsBtnItem = [[UIBarButtonItem alloc] initWithImage:newsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadHome)];
UIImage *weatherImg = [UIImage imageNamed:#"25-weather"];
UIBarButtonItem *weatherBtnItem = [[UIBarButtonItem alloc] initWithImage:weatherImg style:UIBarButtonItemStylePlain target:self action:#selector(loadWeather)];
UIImage *sportsImg = [UIImage imageNamed:#"63-runner"];
UIBarButtonItem *sportsBtnItem = [[UIBarButtonItem alloc] initWithImage:sportsImg style:UIBarButtonItemStylePlain target:self action:#selector(loadSports)];
UIImage *entrnImg = [UIImage imageNamed:#"178-city"];
UIBarButtonItem *entrnBtnItem = [[UIBarButtonItem alloc] initWithImage:entrnImg style:UIBarButtonItemStylePlain target:self action:#selector(loadEntertainment)];
UIImage *videoImg = [UIImage imageNamed:#"69-display"];
UIBarButtonItem *videoBtnItem = [[UIBarButtonItem alloc] initWithImage:videoImg style:UIBarButtonItemStylePlain target:self action:#selector(loadVideoPage)];
UIImage *moreImg = [UIImage imageNamed:#"259-list"];
UIBarButtonItem *moreBtnItem = [[UIBarButtonItem alloc] initWithImage:moreImg style:UIBarButtonItemStylePlain target:self action:#selector(loadOptionsPage)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
[_bottomBar setItems:[NSArray arrayWithObjects:flexibleSpace,newsBtnItem,flexibleSpace,
weatherBtnItem,flexibleSpace,
sportsBtnItem,flexibleSpace,
entrnBtnItem,flexibleSpace,
videoBtnItem,flexibleSpace,
moreBtnItem,flexibleSpace,
nil]];
[self.view addSubview:_bottomBar];
}
- (void)loadWeather
{
NSLog(#"Loading weather");
}

Resources