繁体   English   中英

如何在iOS中处理不同的方向

[英]How to handle different orientations in iOS

添加:您可以在github ios6rotations上访问此项目


抱歉,有人问有关iOS 6屏幕旋转的问题,但这确实让我很痛苦。而且我仍然无法完全理解它-由于某些原因,它在某些情况下的行为有所不同。

我的测试应用程序中具有以下简单的视图层次结构:

我要实现的目标是- 将蓝色控制器仅横向放置,将红色控制器仅纵向放置

我有一个UINavigationController的子类,里面有这样的代码:

@implementation CustomNavController

- (BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return  [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

在我的蓝色控制器中,我实现了这一点:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

在红色控制器中:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

现在,我有以下行为:

  1. 应用在横向模式下启动(确定)
  2. 当我按下按钮时,我的红色控制器也按横向显示(这不行,因为必须在“人像”中显示)
  3. 它成功旋转为纵向但不向后旋转为横向
  4. 如果我将红色控制器留在人像模式下,则我的蓝色控制器(仅限横向)会以人像模式显示。

PS我所有的旋转方法(上面发布的)都被正常调用(顺便说一下,为什么这些方法在每次屏幕转换时都会被调用多次-5-6次)

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation不会通过推送被调用

所有(除portraitUpsideDown外)方向都包含在plist中。

问题是-如何在每个控制器中强制旋转到支持的方向?

我建议您在此处发布(作为答案)任何100%正常工作的代码来处理ios6中的旋转(例如,如果您的iPad使用SplitController的话)-我会将这个问题保留在收藏夹中,以在需要时将它们全部放在一个位置处理一些特定情况。 干杯!

添加:请不要将此作为从风景到肖像的答案发布我希望有一种更优雅的方法。

使用-[UIDevice setOrientation:]是私有API,将使您的应用程序被拒绝。 看到这个问题

您使用公共API无法实现您的要求,并且从HIG的角度来看也不建议您这样做。 受支持并且您应该实现的是具有不同受支持接口方向的不同视图控制器的模式表示。 这就是为什么UINavigationController的默认实现是始终旋转的原因。 假定所有视图控制器都具有相同的受支持接口方向。

以在iPhone上播放视频为例。 打开视频应用程序(iOS随附)。 根视图控制器仅支持纵向方向。 但是,开始播放视频,然后弹出一个模态视图控制器,该控制器仅支持横向界面方向。 这似乎正是您希望实现的行为。

这就是为什么不调用preferredInterfaceOrientationForPresentation原因。 preferredInterfaceOrientationForPresentation仅在使用presentViewController:animated:时被调用。

一个小难题,如果场景的每个阶段都需要导航栏,则需要用导航控制器将每个模态视图控制器封装起来。 然后,您可以通过访问topViewController中导航控制器对象的topViewController来在prepareForSegue:传递所需的数据。


这是一个示例项目,可以根据您的要求正确运行(或者至少会为您提供实现的思路):

http://www.mediafire.com/?zw3qesn8w4v66hy

我的两分钱价值。

您可以快速显示一个空的透明模式视图,然后将其关闭,这可以作为快速解决方法,方法是在ViewController和ViewControllerSecond类的ViewDidLoad:或viewWillAppear:上。

此外,在情节提要中,您可以将ViewController类的方向设置为在视觉上呈现风景

使用此行以编程方式更改方向... 工作100%

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

并且在那时添加此行时,会出现一个警告,要删除此警告,只需在实现文件的顶部添加以下代码。

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

然后在波纹管方法中,如果需要,只需编写此代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return your supported orientations

 if (currentMainView==blueOne) {
        return toInterfaceOrientation== UIInterfaceOrientationPortrait;
    }
}

我的一个应用程序中也有类似的情况(尽管请注意,我没有使用UINavigationController)。

更改shouldAutorotate两个你viewControllers的方法:

//in blue (landscape only)
-(BOOL)shouldAutorotate{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        return YES;
    }
    else {
        return NO;
    }
}

//in red (portrait only)
-(BOOL)shouldAutorotate{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
        //note that UIInterfaceOrientationIsPortrait(self.interfaceOrientation) will return yes for UIInterfaceOrientationPortraitUpsideDown
        return YES;
    }
    else {
        return NO;
    }
}

保持supportedInterfaceOrientations方法相同。

#pragma mark- Orientation Delegate Method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{   Orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (Orientation == UIInterfaceOrientationLandscapeLeft || Orientation == UIInterfaceOrientationLandscapeRight)
    {

        // self.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width,1200);

        [scrollView setScrollEnabled:YES];
        [scrollView setContentSize:CGSizeMake(768, 2150)];


    }else if (Orientation == UIInterfaceOrientationPortrait || Orientation == UIInterfaceOrientationPortraitUpsideDown)
    {

        [scrollView setScrollEnabled:YES];
        [scrollView setContentSize:CGSizeMake(768, 1750)];

    }

}

为了将导航与方向一起使用,您应该使用一堆像数组一样的视图控制器。

结帐之后,以下方法如下:

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

这种方法的改变将对您有很大帮助。

享受编程!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM