8

In my application,I need to show the previous viewController title to current viewController back title.

Its working perfectly in iOS6.

In iOS7,automatically the "back" title displayed other than the previous viewController title.

how to fix the issue in iOS7?

Code cracker
  • 3,105
  • 6
  • 37
  • 67
Richard
  • 139
  • 1
  • 3
  • 11

4 Answers4

29

In iOS 7 you will not be allowed to set the back button's title to be any longer than 11 characters.

To avoid changing the title of the view controller, but to change the back button's title, you need to do this:

In the previous view controller (the one that will have the next view controller pushed on top of it) you need to set the backBarButtonItem like so:

/**
 *  Notifies the view controller that its view was added to a view hierarchy.
 *
 *  @param  animated                    If YES, the view was added to the window using an animation.
 */
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.title = @"My Title Can Be Long";

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"ThisIsLimit"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:nil
                                                                            action:nil];
}

Now, when the next view controller is pushed on top of it, the back button will be whatever title you put in the backBarButtonItem.

Infinity James
  • 4,667
  • 5
  • 23
  • 36
  • I tested your code but the issue not solved.Can you give any other ideas? – Richard Nov 29 '13 at 13:05
  • 1
    My code works. I've used it in an app. You need to put it in the previous view controller, not the new one. – Infinity James Nov 29 '13 at 14:25
  • James Thank you so much.I got the result. – Richard Nov 29 '13 at 14:56
  • 3
    Could you accept the answer please? If I'm honest I want the points or whatever, haha. – Infinity James Nov 29 '13 at 15:25
  • That 's surely the answer. You just have to make it clear that we have to put your code in the **previous** view controller and not in the one that you expect to show the right title. – arniotaki Apr 02 '14 at 20:40
  • 2
    What's the source of "11 characters"? My experience is that it's not a fixed value, it rather depends on the length of the title in the navigation bar. – keeshux Sep 10 '14 at 13:05
11

Due to low reputation I cannot add a comment so I'm posting an answer while this is not actually an answer.

But,

self.navigationController.navigationBar.topItem.title = @"";

which is written in one of the answers, is equivalent to:

self.title = @"";
Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
6

try this,

self.navigationController.navigationBar.topItem.title = @"";
shankar
  • 632
  • 4
  • 14
4

iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.

if you want to make short title you can do as below

self.title = @"SOME REALLY LONG NAVIGATION BAR TITLE";
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
label.text=self.navigationItem.title;
label.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=label;
Sabareesh
  • 3,585
  • 2
  • 24
  • 42
  • I was implemented your code but the problem isn't solved.The "back" title is displayed.Can you give any other suggestion? – Richard Nov 29 '13 at 11:22
  • Can i set the localization to "back"? – Richard Nov 29 '13 at 13:04
  • UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"NewTitle" style: UIBarButtonItemStyleBordered target: nil action: nil]; [[self navigationItem] setBackBarButtonItem: newBackButton]; [newBackButton release]; – Sabareesh Nov 29 '13 at 14:15
  • How do you calculate width of the label so it looks nice on iPhone/iPad both in portrait and landscape orientations? – Alex Sorokoletov Sep 11 '14 at 05:23