强制设备旋转,带+ attemptRotationToDeviceOrientation失败 [英] Force device rotation with +attemptRotationToDeviceOrientation failing

查看:3505
本文介绍了强制设备旋转,带+ attemptRotationToDeviceOrientation失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 UIViewController 这里的文档,

https://developer.apple.com/library /ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation

使用 [UIViewController attemptRotationToDeviceOrientation] 强制系统尝试将界面旋转到新的方向。我试图通过以下方式强制界面从纵向旋转到横向:

using [UIViewController attemptRotationToDeviceOrientation] forces the system to attempt to rotate the interface to a new orientation. I am attempting to force an interface rotation from portrait to landscape by:


  • 设置 forceLandscape

  • 执行<$>


  • < c $ c> -supportedInterfaceOrientations 在我的视图控制器
    中,并检查一个标志来更改支持的方向,例如

  • setting a forceLandscape flag before attempting the rotation
  • Calling [UIViewController attemptRotationToDeviceOrientation]
  • implementing -supportedInterfaceOrientations in my view controller and checking a flag to change the supported orientations, e.g.

这会强制 -supportedInterfaceOrientations 再次调用,看起来像

This forces -supportedInterfaceOrientations to be called again, which looks like

- (NSUInteger)supportedInterfaceOrientations {
    return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}

即使 -supportedInterfaceOrientations 正在调用并返回新方向,接口不旋转。我已经确认该项目允许所有方向,并且该视图控制器是窗口的根视图控制器。有没有人遇到这个问题,并找到一个解决方案?我知道所有的黑客来解决这个(提出和解散模态,等),但我想要一个干净的解决方案,如果可能的话。我已在iOS 6和7上验证了此问题。

Even though -supportedInterfaceOrientations is being called and returns the new orientation, the interface does not rotate. I have confirmed that the project allows all orientations and that this view controller is the window's root view controller. Has anyone else run into this problem and found a solution? I am aware of all the hacks to fix this (presenting and dismissing modals, etc), but I would like a clean solution if possible. I have verified this issue on iOS 6 and 7.

以下是要复制的完整代码:

Below is the complete code to reproduce:

@interface ALTViewController ()
@property (nonatomic, assign) BOOL forceLandscape;
@end

@implementation ALTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.forceLandscape = NO;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button sizeToFit];
    [self.view addSubview:button];
    button.center = self.view.center;
    [button addTarget:self
               action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonPressed:(id)sender
{
    self.forceLandscape = YES;
    [UIViewController attemptRotationToDeviceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}


推荐答案

调用attemptRotationToDeviceOrientation接口的方向,如果且仅当,设备本身已被旋转。

A call to attemptRotationToDeviceOrientation only rotates the orientation of the interface, if and only if, the device itself has been rotated.

例如,如果您的视图控制器仅支持纵向,并且用户将设备旋转为横向,则不会发生接口旋转。当设备是横向时,让我们假设你的代码切换一个允许横向的标志。接口方向会发生什么?没有,因为设备方向已经发生。要获得与设备方向匹配的接口方向,您需要调用attemptRotationToDeviceOrientation。

If your view controller only supported portrait orientation, for example, and the user rotated the device to landscape orientation, no interface rotation would occur. While the device was in landscape orientation, let's say your code toggles a flag which allows for landscape orientation. What would happen to the interface orientation? Nothing, because device orientation has already occurred. To get the interface orientation to match the device orientation, you would need to call attemptRotationToDeviceOrientation.

在我自己的代码中,我最近创建了一个自定义的多部分视图动画,如果界面旋转发生在它的中间,看起来不正确。所以我在简短的动画过程中关闭了界面旋转。如果用户在动画期间旋转设备,则不会发生界面定向。但是,当重新打开界面方向时,在我调用attemptRotationToDeviceOrientation之前,界面将不会旋转以匹配设备方向。

In my own code, I recently created a custom, multi-part view animation that didn't look right if interface rotation occurred in the middle of it. So I turned off interface rotation during the brief animation. If the user rotated the device during the animation, no interface orientation would occur. However, when interface orientation was turned back on, the interface would not rotate to match the device orientation until I called attemptRotationToDeviceOrientation.

这篇关于强制设备旋转,带+ attemptRotationToDeviceOrientation失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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