2014-02-23 51 views
0

我使用導航欄我的應用程序。在iOS7,我的導航leftButtonItem圖標顯示正常(http://prntscr.com/2vaha7)。但在iOS6的,它看起來就像是:http://prntscr.com/2vafer的UIBarButtonItem看起來壞在iOS6的

我的代碼:

_item2 = [[UINavigationItem alloc] init]; 

_item2.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sliderMenu.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)]; 

[self.navigationBar pushNavigationItem:_item2 animated:NO]; 

我怎樣才能在iOS6的解決這個問題的視覺,而不是作品也未嘗不可?

最好的問候,

Onder。

回答

2

看看:http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6

和:http://iosdevblog.com/2013/01/26/the-recommended-size-for-custom-uibarbuttonitem/

您需要爲您的按鈕創建一個自定義透明的背面圖像,並將其設置爲欄按鈕爲iOS 6的背景圖像:

[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
[[UIBarButtonItem appearance] setBackgroundImage:clearImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; 

//編輯:我首先以爲它是一個後退按鈕,您需要像下面這樣調整大小:

UIImage *buttonBack30 = [clearImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 6)]; 
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

我創建clearImage用下面的代碼,但你完全可以創建任何顏色的34x60圖像:

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGRect)rect { 
// Create a 1 by 1 pixel context 
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); 
[color setFill]; 
UIRectFill(rect); // Fill it with your color 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return image; 

}

UIImage *clearImage = [self imageWithColor:[UIColor clearColor] andSize:CGRectMake(0, 0, 34, 60)]; 

    [[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 
    [[UIBarButtonItem appearance] setBackgroundImage:backgroundImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault]; 
+0

謝謝你,它看起來像它要解決我的問題,所以我要在我的應用程序適用於整個ButtonItem代替iOS6的 –

+1

正確的作品你可以申請的,僅適用於iOS 6: 如果([[[的UIDevice currentDevice] systemVersion]的floatValue] <7) – Diwann