willRotateToInterfaceOrientation无法在Universal MasterDetail应用程序中运行

时间:2012-09-10 17:43:26

标签: objective-c ios xcode ipad

所以我有这个代码来改变我的UIImage视图上的图像。我在iPad和iPhone上都有一种非常奇怪的行为。

这段代码完美无缺,给了我想要的结果:

- (void)viewWillAppear:(BOOL)animated {
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }
}

但是在另一种方法中放置的完全相同的代码并没有给我想要的结果!它只需要肖像照片并将其用于风景。至于纵向方向,它使用完全不同的图像!

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)
toInterfaceOrientation duration:
(NSTimeInterval)duration {

    if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeRight||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationLandscapeLeft) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundLandscape.png"];
    } 
    else if ([[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortrait||
        [[UIApplication sharedApplication] statusBarOrientation] ==  UIInterfaceOrientationPortraitUpsideDown) {
        self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];
    }

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

当我为第self.backgroundView.image = [UIImage imageNamed:@"roadBackgroundPortrait.png"];行设置断点时,如果方向为横向,则会触发甚至

我没有太多使用Universal应用程序的经验。我刚刚采用了Apple的Master-Detail App Template并对其进行了一些修改。这可能有什么问题?提前谢谢!

1 个答案:

答案 0 :(得分:1)

使用 toInterfaceOrientation 确定willRotateToInterfaceOrientation方法中的方向。当调用此函数时, statusBarOrientation 仍然是旧函数。

为了您的信息,在更改设备轮换后调用 didRotateFromInterfaceOrientation 。如果您在此处查询 statusBarOrientation ,则应该是正确的。