UIBackButton Background Image not appearing - ios

i'm having a problem with my UIBackbutton not appearing and i'm not sure what is causing it.
This is my code for making the back button image, it is in my first ViewController :
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"backButtonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
When you first enter the next view (done by a segue from a button) the button isn't visible but the text to go back is visible. Once i've hit the back button the image appears and it stays appeared on the next time i click it.
I'm not sure if this is an issue to do with my code, or the file it's in or if it's and iOS 7 problem.

You may have more luck using a UIBarButtonItem and setting the back button background explicitly.
The appearance proxy has been unreliable in my own attempts to get a back button working with iOS 7. I experienced the same issue with the background not showing up properly first time showing the button.

If you just need to change the colour, try this[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

If you want to change back button image you just need to write this 2 lines of code:
[self.navigationBar setBackIndicatorImage:[UIImage imageNamed:#"icon-back"]];
[self.navigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"icon-back"]];
And if you create sub class of UINavigationController it makes easy to customize you navigationbar and push & pop controllers

Try this:
self.navigationItem.hidesBackButton = NO;
if you're trying to add it to the Navigation bar then:
UIImage* image4 = [UIImage imageNamed:#".....png"];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[button setBackgroundImage:....png forState:UIControlStateNormal];
[button addTarget:self action:#selector(Selector)
forControlEvents:UIControlEventTouchUpInside];
[button setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *action = [[UIBarButtonItem alloc] initWithCustomView:button];
NSArray *arr =[[NSArray alloc]initWithObjects:action, nil];
self.navigationItem.rightBarButtonItems = arr;

Related

navigating bar item icon not showing correctly

navigation bar item icon colour blue or single colour. Not able to render the original image in the navigation bar. already tried the option render as original from the Assets.xcassets attributes inspector
want to use thisimage
but sadly when tried selecting through storyboard it shows it in only one single colour square i.e only blue , white or any other colour. how can i use the original image for bar button item.
As per my understanding, you want to set image on navigation bar right ?
Hope below code will wok for you,
UIImage *myImage = [UIImage imageNamed:#"myImageName.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:#selector(menuObject:)];
self.navigationItem.leftBarButtonItem = searchButton;
Try this ...
This one works for me...
UIButton *btnChat = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnChat setBackgroundImage:[UIImage imageNamed:#"chat_icon.png"] forState:UIControlStateNormal];
[btnChat addTarget:self action:#selector(chatButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnChat];

UIBarButtonItem to go back is not showing up?

I have no idea why the back button is not being shown after i press a cell on a table. Now i read that on the parent controller (where you click on the cell to move on to the next view) that you should insert this code:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
self.navigationController.navigationBar.tintColor = navBarItems;
And this is how the setup is done:
So my question is why is the back button item not showing up when i click on a cell?
Note:
The funny thing is that i am using a UIPageView where the back button is showing where i use the same code as i did in the parent view controller. So what am i missing? AND i know for sure that is was working on the previous IOS versions. This happened after upgrading maybe that will help you guys a bit?
Thank you
The back button is not being shown because, in the current UINavigationControllerthere is only the cell detail screen so it has nothing to go back to.
I think that you should put that UINavigationController in front of the UIViewController that holds your UITableView
Try This
UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aBtn setImage:[UIImage imageNamed:#"YourImageName"] forState:UIControlStateNormal];
[aBtn setFrame:CGRectMake(0, 0,50, 64)];
[aBtn.titleLabel setFont:[UIFont systemFontOfSize:18.0]];
aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
aBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
aBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[aBtn setTitleColor:[UIColor colorWithRed:1/255.0 green:65/255.0 blue:96/255.0 alpha:1.0] forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarBtn = [[UIBarButtonItem alloc]initWithCustomView:aBtn];
self.navigationItem.leftBarButtonItem = aBarBtn;
-(void)popViewController
{
[self.navigationController popViewControllerAnimated:YES];
}

Reloading navigation bar

How can I reload/reassign navigation bar items? I use some libraries that change navigation bar and sometimes I have a bag in which all navigation items disappear. I have reassigned right items in viewWillAppear like:
UIButton *actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
actionButton.frame = CGRectMake(270, 0, 50, 50);
actionButton.adjustsImageWhenHighlighted = YES;
[actionButton setImage:[UIImage imageNamed:#"share.png"] forState:UIControlStateNormal];
[actionButton setImage:[UIImage imageNamed:#"share-active.png"] forState:UIControlStateHighlighted];
[actionButton addTarget:self action:#selector(presentActivity) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *actionBarButton = [[UIBarButtonItem alloc]initWithCustomView:actionButton];
actionBarButton.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = actionBarButton;
But it does not work and sometimes I do not have any navigation items.
UIBarButtonItem
is not child of UIButton.
here is not known methods like setImage:forState: etc.
create and custon an UITabBarButton like this
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
newBackButton.image = [UIImage imageNamed:#"back"];
self.navigationItem.leftBarButtonItem=newBackButton;
also verify if method with this code will be called when viewDidLoad
Check out this like https://developer.apple.com/library/ios/documentation/uikit/reference/uinavigationbar_class/Reference/UINavigationBar.html the apple dev is awesome.
This is another good link
AppCoda has a lot of great tutorials this one has everything you need to know about Nav Bars
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
and you could also use perform segue with identifier method and to send a string or something to a new view controller and then when you see that string is equal to what you want use an if statement in your viewWillAppear method.

Navigation Bar Item stretching image for button

I have setup a Navigation Bar in a storyboard with an UIBarButton for the right.
This is the code I use to update the image for this:
// Setup the right navigation bar item
[self.addGameButton setBackgroundImage:[UIImage imageNamed:#"addGameButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addGameButton setTitle:#""];
The image is here, it is 76x58 (at 2x). It is 38x29 (at normal).
When run on the device the image is stretching and I do not know why?
The backgroundImage property is meant to draw a stretchable image for the background of the button. If you want the image inside the button, you could try initializing the UIBarButtonItem with this:
UIImage *image = [UIImage imageNamed:#"images/global/add"];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleBordered target:nil action:nil];
[self.navigationItem setRightBarButtonItem:barButtonItem];
For you, it looks like you have an image that you want to replace the whole button with. You could also try this code, which will create a custom button as the view for your UIBarButtonItem:
UIImage *addImage = [UIImage imageNamed:#"images/global/add"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(0, 0, addImage.size.width, addImage.size.height)];
[addButton setBackgroundImage:addImage forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setRightBarButtonItem:barButtonItem];
In storyboard you can dragg a UIButton over the UIBarButtonItem and you can customise the UIButton as you like.
Try setting the frame of the button to the size you want.
Stuart, since you have setTitle:#"" the nav bar button will not display the text "add" but instead take up the space that was allocated to the text.. I am also yet to find a proper solution but a workaround for your problem is to set the font size to 0.1f. This will resolve your issue..
you need to set the navigation bar appearance in the "application:didFinishLaunchingWithOptions” method of AppDelegate.m, try adding the following code:
UIImage *buttonback = [[UIImage imageNamed:#"nav_back"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 22, 0, 0)];
[[UIBarButtonItem appearance]
setBackButtonBackgroundImage:buttonback forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary
dictionaryWithObjectsAndKeys:[UIColor whiteColor],
UITextAttributeTextColor, [UIFont fontWithName:#"verdana" size:0.1f],
UITextAttributeFont, nil] forState:UIControlStateNormal];
Also Note that this will work with iOS 6, looking for a solution for compatibility in iOS 5

Create UIBarButtonItem with back button style

I'm looking for a way to create programmatically an UIBarButtonItem that looks like a back button of UINavigationBar.
Apparently seems like that the back button appears only after a push on the UINavigationController.
So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.
Ideas ?
the short answer is you cannot do it.
you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar.
I think you need to use image. and then set the image be the buttons backgroundImage. such as :
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
It gives you a psd file with image that you need.
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(custom text with back arrow)

Resources