Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom titleView on UINavigationController is animating incorrectly

I'm probably doing something wrong here because this looks a bit stupid.
I'm setting up a custom titleView (in the form of a UILabel) on my UINavigationController that is the same on every page. To facilitate this, I've created a function in my app delegate to display the label correctly. I then call this function on any subviews just after I push it to the navigation stack.
Here's the code (which probably makes more sense than my explanation):

//In MyAppDelegate.m:
- (void)showTitleForNavigationController:(UINavigationController*) navController {
    UILabel *label = [[UILabel alloc] init];
    // set up label attributes
    // ...
    [label sizeToFit]; //without this line my label won't show at all
    [navController.navigationBar.topItem setTitleView:label];
    [label release];
}

// In SomeViewController.m, when pushing another controller onto the stack:
    UIViewController *otherViewController = //initialize other view controller;
    [self.navigationController pushViewController:otherViewController animated:YES];
    [(MyAppDelegate*)[[UIApplication sharedApplication] delegate] showTitleForNavigationController:otherViewController.navigationController];

My problem is that when I push the next view controller onto the stack, and the new controller slides across smoothly, for the whole duration of the animation the label sticks to the top left before finally snapping into place after the animation is finished. It looks really odd and ugly. How can I set up the label properly so that it slides from the next view smoothly? Surely it's something simple that I'm missing...

like image 270
death_au Avatar asked Sep 13 '10 00:09

death_au


1 Answers

A very late answer to this question, but I just ran into the same problem and found another way to solve it, without using an image. Thought I'd share my solution as it might help someone.

In my case I'm setting a custom UILabel to be the titleview, and I realized that only when I set the titleview property in the viewDidLoad method, it animates correctly. In some cases however, i didn't knew the title yet in my viewDidLoad (in some cases i needed to use a title from a http request for example). So, my solution for those cases was to set the titleview property to my customlabel with text @" " in viewDidLoad, and whenever I got the real title I only changed the text property of my custom label.

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.

   //set temporary title, the MBMUINavigationBarTitleView is a UIView subclass whose  viewWithTitle method returns an autoreleased UIlabel with my custom settings, custom font etc.
   self.navigationItem.titleView = [MBMUINavigationBarTitleView viewWithTitle:@" "];
}

//somewhere later, when I have the real title
UILabel* titleLabel = (UILabel*)self.navigationItem.titleView;
[titleLabel setText:theRealTitle];
like image 138
ylva Avatar answered Nov 07 '22 14:11

ylva