views:

582

answers:

4

I'm using a UILabel for the titleView of a navigation bar (I'm making simple in-app web browser). It works fine, except that when I present a modal view controller, the titleView shifts from the center of the navbar to the far left (underneath the back button). I've tested in 3.0 and up. Here is relevant code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Title view label
    CGRect labelFrame = CGRectMake(0.0, 0.0, 120.0, 36.0); 
    UILabel *label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.numberOfLines = 2;
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(0.0, -1.0);
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    self.navigationItem.titleView = label;
}

-(void)displayComposerSheet:(NSString*)mailto 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [self presentModalViewController:picker animated:YES];
    [picker release];
}

Screenshots: http://img689.imageshack.us/img689/7968/titleviewshift.jpg

Any idea why this is happening? Thanks.

+1  A: 

Does it animate? It may be animating the title view as though it's transitioning to a new view. I don't see anything wrong with your code as written.

I would suggest in your displayComposerSheet, you just unset the titleView, or animate the alpha of the titleView to 0.0. Then, animate it back to 1.0 when you dismiss the modal view controller. Not ideal, but it may look better that way.

Frankly, the whole UINavigation system is crap. We went ahead and re-wrote it ground up because of bizarre issues like these.

DougW
Yes, the modal view is animated (the second screenshot shows the presentation of the modal view in progress). The problem is that the titleview remains left-aligned even after the modal view is dismissed.
jm333
Thanks for the suggestion. I tried a few variations and found a hack that "solves" the problem. The code is in my answer. I'd upvote you but I don't have enough reputation.
jm333
Glad I could help. Yeah, the UINav stuff is a serious boondoggle. It's a shame that we have to resort to hacky fixes like that.
DougW
A: 

Hi,

The only problem is your frame size. so u have to change it.

Try this one.

   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.0)];

   label.font = [UIFont boldSystemFontOfSize:14];
   label.numberOfLines = 2;
   label.backgroundColor = [UIColor clearColor];
   label.textAlignment = UITextAlignmentCenter;
   label.textColor = [UIColor whiteColor];
   label.shadowColor = [UIColor blackColor];
   label.shadowOffset = CGSizeMake(0.0, -1.0);
   label.lineBreakMode = UILineBreakModeMiddleTruncation;
   label.text=@"Stack Overflow";  
   self.navigationItem.titleView = label;
Pugal Devan
Hmm. So I tried changing the width to 320, but the same thing happens. It seems to disregard the frame of the UILabel - instead it left aligns the the text in the label to the left edge of the screen :-/
jm333
+1  A: 

Thanks to DougW for pointing me in right direction. Here's the best hack I found. Basically I retain the UILabel as a class property. Before presenting modal view I unset the titleView, and then reset it immediately after. When the modal view is dismissed I unset then reset the titleView. To the user none of this is visibly notable.

-(void)displayComposerSheet:(NSString*)mailto 
{
    self.navigationItem.titleView = nil;
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    picker.navigationBar.tintColor = [APPDELEGATE getNavTintColor];
    [picker setToRecipients:[NSArray arrayWithObject:mailto]];
    [self presentModalViewController:picker animated:YES];
    [picker release];
    self.navigationItem.titleView = titlelabel;
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{   
    self.navigationItem.titleView = nil;
    self.navigationItem.titleView = titlelabel;
    [self dismissModalViewControllerAnimated:YES];
}
jm333
A: 

I looked into the problem with some hit and try and found the following facts:

  • If the UINavigationBar doesn't have the rightBarButtonItem, the titleView shifts towards the right by ~30pts.
  • It could be reproduced for leftBarButtonItem. But I haven't tried.

In a scenario where the a default UINavigationBar's (with no changes to rightBarButtonItem defaults) titleView is set. And then a new UIView is pushed to the navigation stack which HAS a rightBarButtonItem. Now, if this view is popped [with back button], the navigation bar will remove the rightBarButtonItem. And this will account for the weird offset that shifts the titleView towards a side.

How I fixed the problem was like this:

self.navigationItem.titleView = myCustomTitleView;

// Fake right button to align titleView properly.
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 1)]];
// Width equivalent to system default Done button's (which appears on pushed view in my case).
rightBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem = rightBarButtonItem;

Everything is sweet now. yummmm.

Gurpartap Singh