0

我正在开发具有横向和纵向两种位置的 iOS 应用程序。

使用 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。

4

1 回答 1

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

希望能帮助到你。

于 2012-04-08T18:51:49.200 回答