Setting a custom navigation item backBarButtonItem icon [Objective-c] - ios

I'm trying to set a custom navigation item backBarButtonItem icon.
So far I'm using this code on the preveous view controller's view did load:
- (void)viewDidLoad{
[super viewDidLoad];
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-arrow"] style:UIBarButtonItemStylePlain target:nil
action:nil];
self.navigationItem.backBarButtonItem = myBackButton;}
And here is the result:
I have two icons and I can't figure out what is going on. I want to get rid of the native one and keep mine.
Can someone help me?
UPDATE:
Using:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"back-arrow"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"back-arrow"]];
The result is:
It solves the initial problem, but I got a icon that is too big. How can I resize it? I'm using an icon that is 53x46px.

Try this code:
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"your-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"your-image"]];

Related

UIBarButtonItems with background images

We have a "bottom bar" in our app which at the moment is just a UIView and defined all over the place which leads to inconsistency etc...
I'm trying to replace the use of the this with a UIToolBar and UIBarButtonItems so I can customise the appearance with the UIAppearance protocol.
The buttons we use in the tool bar have a background image made of a resizable image (defined in asset catalog).
What we want it to look like is something like this...
The buttons are nicely centred between the top and bottom of the tool bar.
However, what happens when I set the background image using the UIAppearance protocol is this...
The buttons are vertically aligned off centre.
It seems like the text is in the same place and the background is being applied around the text but the text is actually lower than centre. With no BG this makes sense but with a BG it doesn't work.
I could change the frame in code of every button but this is not ideal as we get back to the inconsistency problem that I'm trying to solve.
Is there any way to change this for all instances of UIBarButtonItem in one place?
I even tried adding transparency to the bottom of the image to "push it upwards" but it didn't have an effect.
Update to show the problem in place
In a enw project I embed a view controller in a navigation controlle rand use this code...
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:animated];
}
- (NSArray *)toolbarItems
{
return #[
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithTitle:#"Hello" style:UIBarButtonItemStyleDone target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithTitle:#"World!" style:UIBarButtonItemStyleDone target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithTitle:#"Goodbye!" style:UIBarButtonItemStyleDone target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
];
}
The result is...
Then I use the UIAppearance code...
[[UIToolbar appearance] setTintColor:[UIColor whiteColor]];
[[UIToolbar appearance] setBarTintColor:[UIColor colorWithRed:0.15 green:0.2 blue:0.85 alpha:1.0]];
And the result is...
This looks fine but when I add the background image...
With the code...
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"ToolBarButtonBackgroundEnabled"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I see this...
I tried changing the title position adjustment...
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -5) forBarMetrics:UIBarMetricsDefault];
But that just moves the text inside the button...
It doesn't move the button background.
I can't find anything relating to the offset of the buttons in the toolbar appearance.
Moving Text
I was wrong about the text staying int he same place. The text moves when you add a background to the bar button items...
I merged screenshots of the toolbar with and without the button backgrounds. You can see that the text is moved downwards.
Have you tried [xyzbtn setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
or your image size is greater than your button's size, so it might be affecting the layout.
I have noticed the same thing: if you set a background image, the UIBarButtonItem moves down by 3pt. It also moves right by 1pt.
This doesn't seem to happen in iOS 11, which is good news.
My conjecture is that Apple made the assumption that if you have a background image set you're trying to indicate a 'selected' button state, and they deliberately move he button down and right a bit to give a 3-D movement appearance.

Mail app segue (open-with) with incorrect navigation bar colors

I am currently attempting to open up the iOS mail all to create a draft with an attachment generated from my app. Here is a code snippet:
NSURL *fileLocation = [NSURL fileURLWithPath:self.site.dataFileLoc];
if(fileLocation) {
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileLocation];
[self.documentController setDelegate:self];
[self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
}
However, what I am finding is that the navigation bar colours are completely off. In the AppDelegate, I am currently using these lines to set the global navigation bar appearance:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navbarBackground"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[UIViewController class], nil] setShadowImage:[UIImage imageNamed:#"shadow"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
Unfortunately, the title of the mail view controller that appears is still black, despite my attempt to set the bar style to black (which should make the title white in return) and blue bar buttons. It does, however, seem to work fine for the "presentPreviewAnimated" view controller. How can I make the navigation bar of the mail view controller consistent?
Are there any nibs associated??
I usually try to avoid the desired level of UI customization (navigation bar styles, colors, custom background w/image) because drawing UI at runtime (unless dynamically required) generally gets me in trouble...
Why not mock it up in a .nib file and make use of the fancy-dance-y tools in Xcode?? Can be therapeutic to quickly obtain the desired grain of detail without having to hit run...

iOS 7 NavigationBar Back Button Custom Image with no Label

I am trying to create a custom back button on a navigation bar. I start from the following:
// Nav bar - back button
[[UINavigationBar appearance] setTintColor:COLOR_WHITE];
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage imageNamed:#"navMenuBackButton"]
imageWithAlignmentRectInsets:UIEdgeInsetsMake(6.0, -6.0, 6.0, -6.0)]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"navMenuBackButton"]];
The image is 34x34 points so does not centre properly without the image alignment. The current problem is trying to get rid of the "Back" label without setting a blank title for each screen or making any changes on the ViewController itself.
Any idea's? Thank you
Disable leftBarButtonItem and rightBarButtonItem. Try the following.
[navigationItem.backItem.leftBarButtonItem setEnabled:NO];
[navigationItem.backItem.rightBarButtonItem setEnabled:NO];
[navigationItem.backItem setHidesBackButton:YES];
Did you try this :
NSString *backString = #"";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backString
style:UIBarButtonItemStyleDone
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];

Back button strangely disappearing in UINavigationController but keeps working

Under iOS7 I've been experiencing an issue where the back button item will not show up if it has been set with a specific background image:
int imageSize = 21; //REPLACE WITH YOUR IMAGE WIDTH
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0)
forBarMetrics:UIBarMetricsDefault];
UIImage *barBackBtnImg = [[UIImage imageNamed:#"BackArrowDark.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Upon doing this, any ViewController that I push in the navigation controller will have no back button item appearing, even though pressing where it should be, will make it appear, and any subsequent pushes of this view controller will have the button present on the screen.
This issue is only appearing under iOS7: everything works perfectly under iOS6.
Changing the back button completely with a leftBarButtonItem disables the back swipe, so that is not an option.
Any idea what I am doing wrong?
Thanks much for your consideration.
After trying different solutions, I found that changing the backIndicatorImage works best under iOS7, and it seems to be in line with the iOS7 interface paradigm:
[[UINavigationBar appearance] setTintColor:[UIColor grayColor]];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; // Takes out title
UIImage *backButtonImage = [UIImage imageNamed:#"BackArrowDark.png"];
if ([UINavigationBar instancesRespondToSelector:#selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:backButtonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backButtonImage];
} else {
int imageSize = 21; // REPLACE WITH YOUR IMAGE WIDTH
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButtonImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
With this method:
When going back in the navigation controller, the back button item transition is the same as with the default indicator (a departure from the back button sliding away as well under iOS6);
Under iOS6, the backButton is changed and keeps its default iOS6 behaviour.
I'm happy!
Make sure you are not calling this in the view controller:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
In iOS 7, this will tint the navigation bar but will also make your buttons invisible, yet functional just as you are describing.

UINavigationController background color changes that hide Back Button in iOS

Now I am trying to change UINavigationController background image with my custom Image.
I can change with following codes.
[navigation.navigationBar insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navController.png"]] atIndex:1];
So I got following pic. That pic is right and show the BarButton.
But when I go to the detailViewController , there is no BackButton. However I can tap without seeing it.
After I back to MainViewController , my UINavigationController Background is happening like following pic.
They are hiding my button. I'm thinking atIndex:1 is making this problem because Index 1 is above 0 and All button Index must be 0.
That's why all buttons are disappearing.
So how can I solve that problem? Please help.
Thanks for your help.
// not supported on iOS4
UINavigationBar *navBar = [purchaseNavController navigationBar];
if ([navBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"brnlthr_nav.jpg"] forBarMetrics:UIBarMetricsDefault];
}
Try using the appearance proxy provided by apple
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"your_image"] forBarMetrics:UIBarMetricsDefault];
that will set it for the entire application or you can use
[[self.navigationController.navigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"your_image"]];

Resources