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];
Related
When I deleted the title text of my back button, the text of my navigation title is not centred.
I used
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
and
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
in both my viewDidLoad and viewWillAppear
this is a screenshot of the result
Any idea how I can fix this?
The UINavigationBar displays the backBarButtonItem of the previous view controller, so you'll need to make sure you are setting that and not the backBarButtonItem of the displayed view controller. The call to setBackButtonTitlePositionAdjustment(_:forBarMetrics:) is not necessary.
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"]];
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.
Note, I've recently come back to this and it seems to have been fixed in more recent versions of the SDK, without me having to implement anything except the code in the question. Many thanks to all who answered.
I have an Objective-C app that runs on the iPad and displays a view controller with a modal presentation style of UIModalPresentationPageSheet:
UINavigationController *editorNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController];
editorNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[navigationController presentViewController:editorNavigationController animated:YES completion:nil];
When this view controller is displayed the buttons in the navigation bar are purple, which I assume has been picked up from the window's tint colour, which is what I want.
Later I need to display another view controller over the top, that fills the whole window:
UINavigationController *previewNavigationController = [[UINavigationController alloc]initWithRootViewController:myPreviewViewController];
[owningViewController presentViewController:previewNavigationController animated:YES completion:nil];
The problem I have is that when myPreviewController is displayed, the buttons in the navigation bar are grey. I've tried reinstating the colour on the new navigation controller:
previewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:123/255.0 green:26/255.0 blue:69/255.0 alpha:1];
but without any joy.
How can I get the buttons to have the correct colour? Can I get the new navigation controller to pick up the window tint colour automatically, or do I have to set it explicitly? Is this something to do with presenting the second navigation controller over the top of one that uses UIModalPresentationPageSheet?
Any help much appreciated! Thanks,
Paul
You can set the navigationBar translucent and transparent.
In view Will Appear:
self.navigationController.navigationBar.translucent = NO;
Than create a UIView with frame size of navigationBar (CGRectMake(0,0,self.view.frame.size.height,22) and create buttons on it with color you need.
I know, thats a crutch but should work)
You can change the appearance of the UIBarButtonItem globally so all UINavigationControllers will share the same design:
[[UIBarButtonItem appearance] setTintColor:[UIColor purpleColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"fontName" size:16.0f],NSFontAttributeName,
nil] forState:UIControlStateNormal];
Additionally you could also change the [UINavigationBar appearance]:
//The setTintColor would tint the < Back button in the NavigationBar
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor blueColor]}];
This code can be add it before presenting the UIViewControllers or simply in the AppDelegate.m.
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.