Search code examples
iosobjective-ccocoa-touchxcode4.5

UITableView controller removes custom NavBar TitleView


I have a bit of code that allows me to customize the Navigation Bar title of a view to allow for shrinking / truncating of long titles. The code works fine almost everywhere, however when it's used in a UITableViewController, the title never appears.

The code in question is:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Nav bar title for truncating longer titles
    CGRect frame = CGRectMake(0, 0, 400, 44);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.minimumFontSize = 10.0f;
    label.lineBreakMode = UILineBreakModeTailTruncation;

    label.text = @"This is a really long title.  It will shrink and eventually get truncated correctly.";
    self.navigationItem.titleView = label;
}

I've put together a small demo project that illustrates the problem. https://sites.google.com/site/coffeestainit/files/NavTitleError.zip


Solution

  • I just went through your project. The problem is that in your storyboard, you have not connected your tableViewController to the FirstViewController. So the viewDidLoad of the FirstViewController is never called. When I set the custom class of your TableViewController to FirstViewController in the storyboard, it set the label as title, as expected