【问题标题】:Unable to detect portrait orientation on iPhone无法在 iPhone 上检测纵向方向
【发布时间】:2011-07-02 03:11:46
【问题描述】:

当我旋转横向时,我正在显示一个模态视图控制器。我想在纵向时删除模态视图控制器。出于某种原因,当我进入纵向模式时,我的日志语句没有出现。

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

【问题讨论】:

    标签: iphone objective-c cocoa-touch uiview uiviewcontroller


    【解决方案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
    }
    }
    

    仅从外观来看,我认为您需要在模态视图内关闭模态视图控制器,而不是在父视图内。因此,您将在主控制器中使用横向版本,然后将“willAnimateRotation...”添加到模态控制器以处理纵向旋转状态。

    【讨论】:

    • 哇,您的代码更简洁,逻辑也正确。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多