2012-04-08 47 views
0

我正在開發和iOS應用程序,包括位置,風景和肖像。用iOS5設置風景圖像到uinavegationBar

使用iOS5,我想設置一個背景圖像到一個自定義的UINavegationBar。

我用的是:

if([self.navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 

    [self.navBar setBackgroundImage:[UIImage imageNamed:@"TabBarContactsPort.png"] forBarMetrics:UIBarMetricsDefault]; 
    [self.navBar setBackgroundImage:[UIImage imageNamed:@"TabBarContactsLand.png"] forBarMetrics:UIBarMetricsLandscapePhone]; 
} 

我寫在viewDiLoad方法的代碼,它的作品確定爲肖像,但使用風景模式相同的圖像。

請幫助我和thaaanks。

回答

1

有條件地設置背景圖片如下:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    if (UIDeviceOrientationIsLandscape(toInterfaceOrientation)) { 
     [self.navBar setBackgroundImage:[UIImage imageNamed:@"TabBarContactsLand.png"] forBarMetrics:UIBarMetricsLandscapePhone]; 
    } 
    else if (UIDeviceOrientationIsPortrait(toInterfaceOrientation)) { 
     [self.navBar setBackgroundImage:[UIImage imageNamed:@"TabBarContactsPort.png"] forBarMetrics:UIBarMetricsDefault]; 
    } 
} 

希望它能幫助。

+0

UIBarMetricsLandscapePhone不起作用。如果我總是使用「forBarMetrics:UIBarMetricsDefault」,您的答案將有效。 – ValentiGoClimb 2012-04-08 20:05:52