Iphone 强制肖像模式

Iphone 强制肖像模式,iphone,ios,objective-c,orientation,Iphone,Ios,Objective C,Orientation,好吧,因为没有人回答我之前的问题,我开始相信可能没有简单的方法可以做到这一点。但我很乐观。这是我的问题: 在我的应用程序中,我使用常规的UI按钮从ViewControllerOne切换到ViewControllerTwo。ViewControllerOne始终处于横向模式。ViewControllerTwo应始终处于纵向模式。但是,当我按下横向ViewControllerOne中的按钮时,ViewControllerTwo也处于横向模式,尽管我希望它切换到纵向模式,无论用户按下按钮时如何旋转设备

好吧,因为没有人回答我之前的问题,我开始相信可能没有简单的方法可以做到这一点。但我很乐观。这是我的问题:

在我的应用程序中,我使用常规的UI按钮从ViewControllerOne切换到ViewControllerTwo。ViewControllerOne始终处于横向模式。ViewControllerTwo应始终处于纵向模式。但是,当我按下横向ViewControllerOne中的按钮时,ViewControllerTwo也处于横向模式,尽管我希望它切换到纵向模式,无论用户按下按钮时如何旋转设备

我向AppDelegate添加了以下代码:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}
我把这个添加到我的ViewController中,它应该只处于纵向模式:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;

}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

-(void)viewWillAppear:(BOOL)animated
{
    UIApplication* application = [UIApplication sharedApplication];
    application.statusBarOrientation = UIInterfaceOrientationPortrait;
}

是否有方法告诉viewController进入纵向模式,即使前一个视图是横向视图?也许我可以创建一个自定义序列,当按下按钮时,它会强制视图处于纵向模式?这里的最佳/官方解决方案是什么?

根据我对这个问题的理解,我假设您希望第二个视图控制器是纵向的,第一个视图控制器是横向的

对于第二个viewcontroller,添加以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}
对于第一个视图控制器:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscape);

}
检查问题并在那里查看答案,以获得您所需的项目示例

基本上,您需要在viewcontroller(要旋转的)中嵌入自定义导航控制器。在此自定义导航控制器中添加以下方法(用于横向,但可替换为纵向)

并添加到应旋转的视图控制器:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
确保在项目中启用了纵向、横向右侧和横向左侧方向。然后,如果要阻止特定视图的某些方向,请执行以下操作:

– application:supportedInterfaceOrientationsForWindow:

现在确定我为什么被降级了。。。这是一个诚实的问题,我没有解决办法。如果您降级,请留下评论。
attemptRotationToDeviceOrientation
的可能重复对您有用吗?如果您希望允许在iOS6+中更改原点,则
–shouldAutorotate
方法应返回
YES
。我建议您阅读定向支持的Apple文档,因为在此处复制相关部分是毫无意义的。可能重复
– application:supportedInterfaceOrientationsForWindow: