iOS:禁用子视图的自动旋转 [英] iOS: Disable Autorotation for a Subview

查看:93
本文介绍了iOS:禁用子视图的自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a nested view hierarchy for an iPad application that supports orientation changes. It looks similiar to the following.

UIViewController
    UIView
        - UIView
            - UIImageView (disable rotation)
            - UIImageView
            - UIView (disable rotation)
        - UIView
        - UIView
        ...

我想锁定某些子视图的方向,同时允许其他人自动旋转和调整大小。我似乎无法弄清楚如何实现这一目标。

I would like to lock the orientation for some of my subviews, while allowing others to auto-rotate and resize. I can't quite seem to figure out how to accomplish this.

一种方法似乎是在 willAnimateRotationToInterfaceOrientation中手动旋转子视图:。鉴于SDK正在执行我刚刚撤消的轮换,这不是特别有吸引力。

One approach seems to be rotating the subviews manually within willAnimateRotationToInterfaceOrientation:. That's not particularly attractive given the SDK is executing a rotation that I would just be undoing.

有没有办法简单地禁用子视图的方向更改或其他一些重组方法我的层次结构?

Is there a way to simply disable orientation changes for subviews or some other method to restructure my hierarchy?

推荐答案

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

Autorotation is handled by a view's UIViewController (shouldAutorotateToInterfaceOrientation:), so one approach is to arrange your hierarchy such that rotatable views are managed by one view controller, and non-rotatable views by another view controller. Both of these UIViewController's root views then need adding to the window/superview.

这里的细微之处在于,如果在同一级别上有两个视图控制器的视图(即通过 addSubview:),只有第一个视图控制器(通常是窗口的 rootViewController )才会收到 shouldAutorotateToInterfaceOrientation: message。

The subtlety here is that if you have two view controller's views on the same level (i.e. added via addSubview:), only the first view controller (usually the window's rootViewController) will receive the shouldAutorotateToInterfaceOrientation: message.

我自己使用这种方法来实现旋转的工具栏,而主视图却没有。

I used this approach myself to achieve a toolbar that rotates, while the main view does not.

Apple的技术Q& A QA1688(为什么我的UIViewController不会随设备一起旋转?)谈谈这个问题。


iOS 6更新:

Autorotation现在使用 UIViewController shouldAutorotate supportedInterfaceOrie ntations 方法。 shouldAutorotate 默认返回 YES ,但请记住 rootViewController以外的视图控制器其视图是窗口的直接子视图,无论如何都不会收到轮换回调。

Autorotation now uses UIViewController's shouldAutorotate and supportedInterfaceOrientations methods. shouldAutorotate returns YES by default, but remember that a view controller other than the rootViewController whose view is a direct subview of the window will NOT receive rotation callbacks anyway.

iOS 6示例代码:

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

Create a new project using the "Single View Application" template, and ensure "Use Storyboards" is checked. We'll use the provided ViewController class as the rotating view controller (rename it if you like!), and create a second UIViewController subclass called NonRotatingViewController. Although this view controller will never even receive the rotation callbacks, for completeness and clarity add the following code in NonRotatingViewController.m:

- (BOOL)shouldAutorotate
{
    return NO;
}

MainStoryboard 文件,拖出一个新的视图控制器对象并将其类设置为 NonRotatingViewController ,并将其Storyboard ID设置为NonRotatingVC。当您在那里时,将旋转视图控制器视图的背景颜色更改为清除(非旋转视图将添加到此视图下方),并为每个视图添加标签。在 AppDelegate.m 中,添加以下代码:

In the MainStoryboard file, drag out a new view controller object and set its class to NonRotatingViewController, and set its Storyboard ID to "NonRotatingVC". While you're there, change the rotating view controller view's background color to clear (the non rotating view will be added underneath this one), and add a label to each view. In AppDelegate.m, add the following code:

#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;
}

这只是实例化非旋转视图控制器并将其视图直接添加到窗口(此时NB已经由故事板设置了窗口的 rootViewController )。

This is just instantiating a non rotating view controller and adding its view directly to the window (N.B. at this point the window's rootViewController has already been set by the storyboard).

运行项目。转动设备并惊叹于一个标签旋转而另一个标签保持静止!

Run the project. Rotate the device and marvel at the sight of one label rotating while the other stays still!

示例代码预iOS 6:

我在一个新项目中做到了这一点 - 一个新的基于视图的应用程序会做得很好。添加两个新的视图控制器: RotatingViewController NonRotatingViewController 。在他们的每个笔尖里面,我只添加了一个标签来描述视图是否应该旋转。添加以下代码:

I did this in a new project - a new View-based Application will do just fine. Add two new view controllers: RotatingViewController and NonRotatingViewController. Inside each of their nibs I just added a label to describe whether the view should rotate or not. Add the following code:

' RotatingViewController.m '

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



' NonRotatingViewController。 m '

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {    // Or whatever orientation it will be presented in.
        return YES;
    }
    return NO;
}



' AppDelegate。 m '

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

我希望这有帮助。

这篇关于iOS:禁用子视图的自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆