[Objective-C]无法检测到在 iPhone 上的纵向方向

发布时间: 2016/9/23 3:17:12
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我只在当我旋转景观展示modal视图控制器。我想要删除的modal视图控制器在画像中的时候。出于某种原因,我日志语句不会出现当我进入纵向模式。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait ||
                   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ||
                   interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
                   interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


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

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {

        NSLog(@"showing chart");
        [self presentModalViewController:landscapeChartViewController animated:NO];
    }

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        NSLog(@"dismissing chart");
        [self.parentViewController dismissModalViewControllerAnimated:NO];
    }
}

解决方法 1:

你可以有点简化了此代码,可能会帮助把它变窄。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return YES; // Return YES is the same as entering all interfaces.
}


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

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {

    NSLog(@"showing chart");
    [self presentModalViewController:landscapeChartViewController animated:NO];
}

if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
    NSLog(@"dismissing chart");
    [self.parentViewController dismissModalViewControllerAnimated:NO];
    // self.parentViewController seems like a call FROM the modalViewController. 
    // This should be moved to the modalViewControllers implementation
}
}

只是从看着它,我想你需要解雇modal视图控制器modal视图里面,不在父视图内。所以,你会里面你主控制器,使用景观版本,然后将"willAnimateRotation..."添加到要处理纵向旋转状态的modal控制器。

赞助商