How to hide Navigation Bar back button title from AppDelegate - ios

Whenever my app receives a push notification with custom payload I'd like to push the app to a UIViewController, I was able to do it but I cannot remove the title from the Back button of the UINavigationBar from the AppDelegate.
What I tried was:
[self.window.rootViewController.navigationController setNavigationBarHidden:NO];
[self.window.rootViewController.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.window.rootViewController.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.window.rootViewController.navigationController.navigationBar setTranslucent:YES];
[self.window.rootViewController.navigationController.navigationBar setTintColor:[UIColor mainBlue]];
self.window.rootViewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:self.window.rootViewController.navigationItem.backBarButtonItem.style target:nil action:nil];
The last line is where I set the title of the back button to nil but it doesn not work, when the UIViewController becomes visible I can still se the title. The code works if I use it on the UIViewController before that one but not on the AppDelegate. Does anyone have a solution? What am I doing wrong?

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
This will hide back button title from all navigation items. You should change the offset value based on your title length.

Related

iOS Navgation bar title not in center?

I am not using any customized bar button items in nav bar, just default title and back button in navigation bar. But in one of my view controller the back button is taking up too much space as highlighted in the image. So it's pushing the Nav Title towards right.
I am not sure what is causing this. Is there anyway to adjust the content inset or width of the back button in ios?
If you using default Back Button on Navigation bar,add my working code to previous viewController while pushing to nextViewController
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
And then add title to navigation bar on nextViewController viewDidLoad: method
[self.navigationItem setTitle:NSLocalizedString(#"Restaurants", nil)];
Handle back button action event from nextViewController like this,
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"G_Back", nil) style:UIBarButtonItemStylePlain target:nil action:#selector(actionToBackBtnFromDetail:)];

Button not show in the navigation bar

I try this code for the navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
and for the button I tried this code
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
self.navigationItem.rightBarButtonItem=add;
after this code i declare SaveButtonClicked method also.
When I try this code there is no error in this code and compiled successfully but the button is not show in the navigation bar.
Help me with right code and suggestions.
After you have created navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
Then You need to create navigation item. and add navigation item to navigation bar
UINavigationItem *item = [[UINavigationItem alloc]init];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
item.rightBarButtonItem=add;
navbar.items = [NSArray arrayWithObjects:item, nil];
[self.view addSubview:navbar];
Hope this Helps!
1) You don't need to create a UINavigationBar. The navigation controller you're in provides that.
2) And if you did think you needed one, that probably means your view isn't in a navigation controller at all. Check what the value of self.navigationItem is. I imagine you'll find it to be nil. That means you need to redo your interface so this view is actually inside a navigation controller. "Embed in Navigation Controller" under "Editor" in Interface Builder may be of assistance there.
you can just use this single line to achieve that:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveButtonClicked:)];
after this add this method
-(void)saveButtonClicked:(UIBarButtonItem *)sender{
}
for more you can refer Add button to navigationbar programmatically

iOS Change NavigationBar appearance throughout entire app

I have multiple UINavigationController throughout my storyboard. Since I am using a tabbarcontroller every tab item has it's own UINavigationController embedded before it's ViewController.
I'd like to style all of these at the same time. Things that I have tried that work are going to a ViewControllers ViewWillAppear method and adding the following lines:
UINavigationBar *nav = self.navigationController.navigationBar;
nav.barStyle = UIBarStyleBlack;
nav.tintColor = [UIColor blackColor];
nav.translucent = NO;
But then I'd have to do this for every tab item and every ViewController.
Also, doing the following in the AppDelegate did NOT work:
[[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
[[UINavigationBar appearance] setTranslucent:NO];
Specifically I am curious why using the appearance proxy doesn't work. I'm fairly new so if you give a solution involving custom UINavigationController or setting up a delegate please elaborate. Thanks!
Add this line too in your code:
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];

Modify the More button on the Navigation bar of a tab displayed from a UITabBarController's More tab

I have an app with a UITabBarController that has more than five tabs.
When I press the More tab, I go to the moreNavigationController which is a UINavigationController.
As you can see, I have figured out how to style the Title, Tint, Table color, and the Edit button on the More screen, as well as the Configure screen from pressing the Edit button.
What I have not been able to figure out is how to style the back button, titled More, when I select an item in the table.
Each tab has it's own class, GRWTabSettingsViewController for example, which inherits from GRWViewController, which provides common functionality for all tabs, which then inherits from UIViewController.
When on the Settings screen (or any other tab), I am trying to edit the More back button.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[(UIBarButtonItem *)[(UINavigationItem *)[(UINavigationBar *)[(UINavigationController *)[self navigationController] navigationBar] topItem] leftBarButtonItem] setTintColor:[UIColor darkGrayColor]];
[self.navigationController.navigationBar.topItem.leftBarButtonItem setTintColor:[UIColor darkGrayColor]];
}
However, this navigationController is clearly the parent since these changes get applied to the More screen and not the Settings screen.
What am I misunderstanding and how would I modify the buttons displayed on the navigation bar of the screen I am viewing?
=== SOLUTION ===
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// use backBarButtonItem not leftBarButtonItem
//[(UIBarButtonItem *)[(UINavigationItem *)[(UINavigationBar *)[(UINavigationController *)[self navigationController] navigationBar] topItem] leftBarButtonItem] setTintColor:[UIColor darkGrayColor]];
//[self.navigationController.navigationBar.topItem.leftBarButtonItem setTintColor:[UIColor darkGrayColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:self.navigationController.navigationBar.topItem.title
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[backButton setTintColor:[UIColor darkGrayColor]];
self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;
// these do not work
//[self.navigationController.navigationBar.topItem.backBarButtonItem setTintColor:[UIColor darkGrayColor]];
//[backButton setTintColor:[UIColor darkGrayColor]];
}
Did take me a while to figure out that I can not format the button through self, or format the button after the assignment to self.
You should be customizing backBarButtonItem of the previous navigation item, not of the topItem.

Trying to change attributes like Title and back button on navigationcontroler

I am trying to set the back button on the navigation controller that currently has my view on the stack.
I can not change any of its attributes.
Is there an way I can grap the top most navigation controller like: [self navigationcontroller]?
By default, the back button in a UINavigationController is owned by the parent.
Simply set the back button before you call pushViewController:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Go Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[self.navigationController pushViewController:myNewController];
(taken from this tutorial)
You can access your NavigationController with self.navigationController. Just as you would when pushing and popping ViewControllers.
By default, your NavigationController grabs the title from your ViewController, so you just have to set that like this;
self.title = #"My title";
And to access the back button;
self.navigationController.backBarButtonItem
UINavigationController Class Reference
Setting title:
[[self navigationItem] setTitle:tabTitle];
setting barButton appearance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Font-Name" size:12.0f], UITextAttributeFont, [UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];

Resources