Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:禁用子视图的自动旋转_Ios_Ipad_Uiview_Orientation_Device Orientation - Fatal编程技术网

iOS:禁用子视图的自动旋转

iOS:禁用子视图的自动旋转,ios,ipad,uiview,orientation,device-orientation,Ios,Ipad,Uiview,Orientation,Device Orientation,我有一个支持方向更改的iPad应用程序的嵌套视图层次结构。它看起来与下面的类似 UIViewController UIView - UIView - UIImageView (disable rotation) - UIImageView - UIView (disable rotation) - UIView - UIView ... 我想锁定一些子

我有一个支持方向更改的iPad应用程序的嵌套视图层次结构。它看起来与下面的类似

UIViewController
    UIView
        - UIView
            - UIImageView (disable rotation)
            - UIImageView
            - UIView (disable rotation)
        - UIView
        - UIView
        ...
我想锁定一些子视图的方向,同时允许其他子视图自动旋转和调整大小。我似乎不太明白如何做到这一点

一种方法似乎是在
WillAnimateRotationInterfaceOrientation:
中手动旋转子视图。考虑到SDK正在执行一个我将要撤消的循环,这并不是特别有吸引力


有没有一种方法可以简单地禁用子视图的方向更改或其他一些方法来重新构造我的层次结构?

自动旋转由视图的UIViewController处理(
应该是AutoRotateTointerFaceOrientation:
),因此一种方法是排列层次结构,使可旋转视图由一个视图控制器管理,和其他视图控制器的不可旋转视图。这两个UIViewController的根视图都需要添加到窗口/超级视图中

这里的微妙之处在于,如果在同一级别上有两个视图控制器的视图(即通过
addSubview:
添加),则只有第一个视图控制器(通常是窗口的
rootViewController
)将收到
shouldAutorotatePointerFaceOrientation:
消息

我自己用这种方法实现了一个工具栏,它可以旋转,而主视图不能

谈一点这个问题


iOS 6的更新:

自动旋转现在使用
UIViewController
shouldAutorotate
支持的界面方向方法<默认情况下,code>shouldAutorotate
返回
YES
,但请记住,其视图是窗口的直接子视图的
rootViewController
以外的视图控制器无论如何都不会收到旋转回调


iOS 6的示例代码:

使用“单视图应用程序”模板创建新项目,并确保选中“使用故事板”。我们将使用提供的
ViewController
类作为旋转视图控制器(如果愿意,请重命名!),并创建第二个名为
NonRotatingViewController
UIViewController
子类。尽管此视图控制器甚至永远不会收到旋转回调,但为了完整性和清晰性,请在
NonRotatingViewController.m
中添加以下代码:

- (BOOL)shouldAutorotate
{
    return NO;
}
MainStoryboard
文件中,拖出一个新的视图控制器对象,将其类设置为
NonRotatingViewController
,并将其脚本ID设置为“NonRotatingVC”。此时,将“旋转视图控制器”视图的背景色更改为“清除”(非旋转视图将添加在此视图下方),并为每个视图添加标签。在
AppDelegate.m
中,添加以下代码:

#import "NonRotatingViewController.h"

// ...
// ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NonRotatingViewController *nonRotatingVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"NonRotatingVC"];
    [self.window addSubview:nonRotatingVC.view];
    return YES;
}
这只是实例化一个非旋转视图控制器,并将其视图直接添加到窗口中(注意,此时窗口的
rootViewController
已由情节提要设置)

运行项目。旋转设备,看到一个标签旋转而另一个标签静止不动,你会感到惊奇


iOS 6之前的示例代码:

我在一个新项目中这样做了——一个新的基于视图的应用程序就可以了。添加两个新视图控制器:
RotatingViewController
NonRotatingViewController
。在每个笔尖内,我只是添加了一个标签来描述视图是否应该旋转。添加以下代码:

#import "NonRotatingViewController.h"

// ...
// ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NonRotatingViewController *nonRotatingVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"NonRotatingVC"];
    [self.window addSubview:nonRotatingVC.view];
    return YES;
}
旋转视图控制器.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {    // Or whatever orientation it will be presented in.
        return YES;
    }
    return NO;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RotatingViewController *rotating = [[RotatingViewController alloc] initWithNibName:@"RotatingViewController" bundle:nil];
    self.rotatingViewController = rotating;
    [rotating release];

    NonRotatingViewController *nonRotating = [[NonRotatingViewController alloc] initWithNibName:@"NonRotatingViewController" bundle:nil];
    self.nonRotatingViewController = nonRotating;
    [nonRotating release];

    [self.window addSubview:self.rotatingViewController.view];
    [self.window insertSubview:self.nonRotatingViewController.view belowSubview:self.rotatingViewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

非旋转视图控制器.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {    // Or whatever orientation it will be presented in.
        return YES;
    }
    return NO;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RotatingViewController *rotating = [[RotatingViewController alloc] initWithNibName:@"RotatingViewController" bundle:nil];
    self.rotatingViewController = rotating;
    [rotating release];

    NonRotatingViewController *nonRotating = [[NonRotatingViewController alloc] initWithNibName:@"NonRotatingViewController" bundle:nil];
    self.nonRotatingViewController = nonRotating;
    [nonRotating release];

    [self.window addSubview:self.rotatingViewController.view];
    [self.window insertSubview:self.nonRotatingViewController.view belowSubview:self.rotatingViewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

AppDelegate.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {    // Or whatever orientation it will be presented in.
        return YES;
    }
    return NO;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    RotatingViewController *rotating = [[RotatingViewController alloc] initWithNibName:@"RotatingViewController" bundle:nil];
    self.rotatingViewController = rotating;
    [rotating release];

    NonRotatingViewController *nonRotating = [[NonRotatingViewController alloc] initWithNibName:@"NonRotatingViewController" bundle:nil];
    self.nonRotatingViewController = nonRotating;
    [nonRotating release];

    [self.window addSubview:self.rotatingViewController.view];
    [self.window insertSubview:self.nonRotatingViewController.view belowSubview:self.rotatingViewController.view];

    [self.window makeKeyAndVisible];

    return YES;
}

我希望这能有所帮助。

这里是另一种方法。只需将此代码放入viewController viewDidLoad:

    YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        // the view you don't want rotated (there could be a heierarchy of views here):
    UIView *nonRotatingView = [[UIView alloc] initWithFrame:CGRectMake(100,0,30,500)];
    nonRotatingView.backgroundColor = [UIColor purpleColor];

        // make sure self.view and its children are transparent so you can see through to this view that will not be rotated behind self.view.

    [delegate.window insertSubview:nonRotatingView  belowSubview:self.view];

        // you can't put it in the front using:
        //    [delegate.window insertSubview:nonRotatingView aboveSubview:self.view];
        // It will show up the same as before

    // you should declare nonRotatingView as a property so you can easily access it for removal, etc.

我解决这个问题的方法是使用
UINotification
来检测自动旋转,并将视图反向旋转

在此处找到完整的代码:


这种方法似乎对我不起作用。我已经使用父视图控制器配置了一个视图层次结构,它包含两个子视图控制器(肖像视图控制器和rotateViewController),每个子视图控制器都包含一个UIImageView,其中shouldAutorotateToInterfaceOrientation为“肖像”返回“否”,为“旋转”返回“是”。如果我在父级中将shouldAutorotateToInterfaceOrientation设置为NO,则方向是静态的,正如预期的那样。但是,在我的父对象中将shouldAutorotateToInterfaceOrientation设置为YES会导致两者都旋转。你能发一些代码吗?我在某个地方出了问题。@Jason:视图控制器可能非常喜怒无常,也很特别-使用您描述的自定义“容器”控制器可能是导致问题的原因。我会确保您的子视图控制器视图都直接连接到应用程序窗口。谢谢StuDev+1四周。“容器”控制器肯定是造成问题的原因。谢谢,这是个棘手的问题,这个解决方案对我来说非常有效。我还想补充一点,分离视图意味着我的可旋转视图需要通过触摸事件。。。这个答案有助于:仅供参考,我解决了创建两个窗口的问题:一个旋转,另一个固定。每个窗口都有自己的根视图控制器-一个固定,另一个旋转:-)这对我来说非常适合!比另一种解决方案更简单。需要有人解释为什么这样做,因为代码没有使其变得明显。非旋转视图放置在应用程序的窗口和viewcontroller视图之间,并且不由viewcontroller管理。viewcontroller将仅处理其视图的方向更改,并将忽略放置在它和窗口之间的此视图。我已实现此操作,因为我的视图控制器中有一个AVCaptureSession,我希望它不旋转,但我希望非旋转视图不旋转