2013-02-25 38 views
0

> An illustration is better than many words如何中心在iOS 6

導航欄標題正如你可以看到我在離開我的另一面的標題和242有217px。

我在iOS 6上只有這個問題,標題完全集中在舊版本。 這裏是我讓我的導航欄的方式:

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

我真的不知道我做錯了,如果有人有任何想法...

其他問題,你知道如何我可以設置我的標題的位置(這次是在y軸上)嗎?

編輯:可以找到一個臨時解決方案here

回答

0

我會建議改變你的resizableImageWithCapInsets值,看看如何將你的標題向左或向右移動。我認爲正在發生的事情是您的文字以「在文本區域內」爲中心,但文字區域由於可調整大小的圖像而被抵消。

+0

更改resizableImageWithCapInsets值不會更改我的標題位置。 – 2013-02-25 20:33:09

0

我有同樣的問題,我的解決方案是消除陰影偏移。我的意思是,該行:

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

我居然沒把我的標題單獨的屬性,但使用此代碼塊(與剿的註釋行):

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

我不知道爲什麼,但是這解決了問題,標題在iOS6中看起來很完美。標題甚至有一個影子偏移量,默認的偏移量。我真的希望這有助於某人。這個簡單的問題讓我瘋狂了好幾個小時。