Trying to change attributes like Title and back button on navigationcontroler - ios

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];

Related

setting title on navigation bar not working

I want to set title to my navigation bar. I'm using
[self.navigationItem setTitle:#"Some Title"];
But, the back button title is changed not the navigation bar title.
self.navigationController.navigationBar.topItem.title = #"myTitle";
or
in .h
IBOutlet UINavigationItem *navItem;
in .m
navItem.title = #"New Title";
[self.navigationController.navigationItem setTitle:#"Title"];
Use this by this only navigation bar's title will change not back button's title
self.navigationItem.title=#"myTitle";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem=backButton;
Did you implement back button like this way if not use this code
Try to set the title of your UINavigationController:
[self.navigationController setTitle:#"Title"];
Adding this to your current view controller:
[self setTitle:#"Your Title"]
or set it up at previous view controller:
destinationViewControl.title = #"Your Title"

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.

Can't overwrite navigation button UICollectionViewController

I have a UICollectionViewController which acts like a paging, each cell has its own page.
On navigation bar, it always shows "Back".
The following wouldn't change the title.
-(void)viewWillAppear:(BOOL)animated{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle: #"Back Button Text"
style: UIBarButtonItemStyleBordered
target: nil action: nil];
[self.navigationItem setBackBarButtonItem: backButton];
You'll need to use this code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Custom Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
Note that you need to set this on the view controller that the back button will point to. For example, if ViewControllerA pushed ViewControllerB onto the nav stack, then you would use this code on ViewControllerA.

Adding a UIBarButtonItem programmatically to UINavigationBar

I dropped in a UINavigationBar in UIInterfaceBuilder. I present this view modally and just want a UIBackBarButton to return to my last view. I have an outlet and property to this UINavigationBar declared. I thought in my viewDidLoad method, I could create a UIBackButton like this:
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered
target:self
action:#selector(goBack)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
But I do not see my UIBackBarButtonItem on the UINavigationBar. I think I am doing something wrong here since I don't think my UINavigationBar knows I'm trying to add this UIBackBarButtonItem to it in this way. Would I have to do create an NSArray, put the button in it, and setItems for the NavigationBar instead?
I'm confused on how the navigationItem property works vs the setItems of the UINavigationBar as well. Any help would be appreciated. Thanks!
You are trying to set the Back Button Item in a modal view which doesn't add a backBarButtonItem. This what causes the Button (or any sort of back button for that matter) not to show. The backBarButtonItem is mainly for use with Pushed View Controllers which have a Back Button added from the parent (next item below) when you push a new view controller (top item). The Apple UINavigationItem Documentation says:
When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.
To get the Back Button on the left side like you wish, Try changing
self.navigationItem.backBarButtonItem = backButton;
to
self.navigationItem.leftBarButtonItem = backButton;
making a call such as this from a view controller
{
NextViewController* vcRootView = [[NextViewController alloc] initWithNibName:#"NextView" bundle:[NSBundle mainBundle]];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:vcRootView];
[vcRootView release];
[self.navigationController presentModalViewController:navController animated:YES];
[navController release];
}
will present NextViewController as a Modal view on the calling view and NextViewController will have a navigationController for it.
In The NextViewController implementation file all you need is this
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self
action:#selector(barButtonBackPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
}
-(void)barButtonBackPressed:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
to have the back button to dismiss the modalview. Hope it helps.
Use below code snippet :
//Add button to NavigationController
UIBarButtonItem *backButton =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#“back”, #"")
style:UIBarButtonItemStylePlain
target:self
action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
//Perform action on back Button
- (void) goBack { // Go back task over-here
}
Different style types available are :
UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered, UIBarButtonItemStyleDone
You may use this setters without creation new UIBarButtonItem:
[self.navigationItem.leftBarButtonItem setAction:#selector(doBackButton:)];
[self.navigationItem.leftBarButtonItem setTarget:self];

How to change text on a back button

By default the back button uses as a text on it a title of a viewcontroller.
Can I change text on the back button without changing a title of a view controller?
I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.
I tried the following which didn't work:
self.navigationItem.leftBarButtonItem.title = #"Back";
Thanks.
Try
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationItem.
Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:
(bottom) ControllerA -> ControllerB (top)
...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.
So it's more in the context of self.title, not like the other left/right-bar-button setters.
You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.
Thanks Marco... that helped me...
Here is what i did.
If you are using a tableView to navigate to different views... put the code:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
In your didSelectRowAtIndexPath method... of the first Controller... Controller A.
When you navigate to Controller B the button will have the title "Back".
The back button pulls its text from the title of the parent view controller.
In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.
For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."
in the viewDidLoad method of RootViewController.m:
self.title = #"Home";
in the viewDidLoad method of SecondViewController.m:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
If you want your back button to read, "Back," set the title of the parent view controller to #"Back";
This work better for me. Try :
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
If you are using storyboard:
Open StoryBoard
In Document Outline window find ViewController to which you want to
return to
Click on Navigation Item of that ViewController
In Attributes explorer change Back Button value to your custom tile
That is it, enjoy...
And in MonoTouch the following works (in ViewDidLoad of the parent controller):
NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];
This worked for me.
In your parent view controller, set the back button when view loads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Notice that we don't need to include autorelease at the end with the latest iOS version.
Hope this helps!
[self.navigationController.navigationBar.backItem setTitle:#"back"];
It works for me. You can replace "back" with something else.
This one worked for me if you don't want to have a title!
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!
self.navigationItem.title = #"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:
So I put the title change in calling view controller when I push the other view controller like:
self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:#"pushedVCIdentifier"];
self.pushedVC.delegate = self;
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = #"Back";
and in delegate callback function (which I invoke in viewWillDissapear):
-(void)pushedVCWillClose:(PushedVC *)sender
{
self.title = #"Previous Title";
}
If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".
//You can achieve this by setting the title in the previous view controller as shown below
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}
For to change back button title, refer below code
InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];
//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`
In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003
This worked for me:
self.navigationController.navigationBar.topItem.title = "Back"

Resources