0

> An illustration is better than many words

As you can see i have 217px on left of my title and 242 on the other side.

I only have this problem on iOS 6, the title is perfectly centered on older versions. Here is the way i make my navigation bar :

- (void)initNavigationBar
{
    //Background
    UIImage *navBarBackground = [UIImage imageNamed:@"navigationBar"];
    [[UINavigationBar appearance] setBackgroundImage:navBarBackground forBarMetrics:UIBarMetricsDefault];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

    //Title
    NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
    [titleBarAttributes setValue:[UIFont fontWithName:@"NeutrafaceText-Bold" size:20] forKey:UITextAttributeFont];
    [titleBarAttributes setValue:[UIColor colorWithRed:17.0/255.0 green:83.0/255.0 blue:144.0/255.0 alpha:1] forKey:UITextAttributeTextShadowColor];
    [titleBarAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0, 1.5)] forKey:UITextAttributeTextShadowOffset];

    [[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];    

    //Buttons
    //Back
    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
    [attributes setValue:[UIFont fontWithName:@"NeutrafaceText-Demi" size:14] forKey:UITextAttributeFont];
    [attributes setValue:[UIColor colorWithRed:161.0/255.0 green:203.0/255.0 blue:238.0/255.0 alpha:1] forKey:UITextAttributeTextColor];
    [attributes setValue:[UIColor colorWithRed:10.0/255.0 green:54.0/255.0 blue:92.0/255.0 alpha:1] forKey:UITextAttributeTextShadowColor];
    [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0, 1.5)] forKey:UITextAttributeTextShadowOffset];

    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
    UIImage *backButtonBackground = [[UIImage imageNamed:@"backButtonBackground"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 12, 1, 12)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(2.5f, 0.0f) forBarMetrics:UIBarMetricsDefault];

    //Right
    UIImage *rightButtonBackground = [[UIImage imageNamed:@"rightButtonBackground"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 10, 1, 10)];
    [[UIBarButtonItem appearance] setBackgroundImage:rightButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}

I really don't know what i'm doing wrong, if someone have any idea...

Other question, do you know how can i set the position of my title (on y axis this time) ?

EDIT : A temporary solution can be found here.

Community
  • 1
  • 1
R.Lambert
  • 1,160
  • 1
  • 11
  • 25

2 Answers2

0

I would suggest changing your resizableImageWithCapInsets values and see how that moves your title left or right. What I think is happening is that your text IS centered "inside the text area", but the text area is being offset because of your resizable images.

Daddy
  • 9,045
  • 7
  • 69
  • 98
0

I had the same problem, and my solution was eliminating the shadow offset. I mean, the line:

[titleBarAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0, 1.5)] forKey:UITextAttributeTextShadowOffset];

I actually didn't set my title attributes separately, but use this code block (and supress the commented line):

[[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:
      [UIColor colorWithRed:248.0/255.0 green:235.0/255.0 blue:227.0/255.0 alpha:1.0], UITextAttributeTextColor,
      [UIColor colorWithRed:67.0/255.0 green:32.0/255 blue:12.0/255.0 alpha:0.8], UITextAttributeTextShadowColor,
      //CGSizeMake(0, 1), UITextAttributeTextShadowOffset,
      [UIFont fontWithName:@"DIN-Bold" size:16.0], UITextAttributeFont,nil]];

I have no idea why, but this solved the problem and the title looks perfect in iOS6. The title even had a shadow offset, the default one. I really hope this helps someone. This simple problem drive me crazy for hours.

Pepper
  • 394
  • 5
  • 10