使用iOS5将风景图像设置为uinavigationBar

时间:2012-04-08 18:41:33

标签: ios5 landscape-portrait

我正在开发iOS应用程序,包括Landscape和Portrait。

使用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方法中编写此代码,它适用于Portrait,但在横向模式下使用相同的图像。

请帮助我和帮助。

1 个答案:

答案 0 :(得分: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];
    }
}

希望它有所帮助。