Question

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...

Was it helpful?

Solution 2

What I ended up doing was using an image with the text included as the backround for the header, so rather than animating smoothly as I wanted originally, it's not animating at all.
Considering it's the same heading everywhere, it's not that big a deal though.

OTHER TIPS

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

I was in a similar situation to ylva, using an instance of a custom text class for the UINavigationItem's titleView property. However, I found configuring it in viewDidLoad didn't resolve the animation glitch.

My workaround to the problem was to wait until the view controller in question was being popped off the navigation controller's stack, and at that point remove the UINavigationItem's custom titleView so it never needs to get animated at all.

When my UINavigationController sub-class receives the popViewControllerAnimated: message, I copy the title text from my custom text field (UINavigationItem's titleView) into the UINavigationItem's title property and set the titleView property to nil. Then the UINavigationController goes ahead and pops off the view controller and only the standard navigation bar title label is animated (not my custom title), glitch free.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top