简体   繁体   English

使用+ attemptRotationToDeviceOrientation强制设备旋转失败

[英]Force device rotation with +attemptRotationToDeviceOrientation failing

According to the UIViewController docs here, 根据这里的UIViewController文档,

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

using [UIViewController attemptRotationToDeviceOrientation] forces the system to attempt to rotate the interface to a new orientation. 使用[UIViewController attemptRotationToDeviceOrientation]强制系统尝试将接口旋转到新方向。 I am attempting to force an interface rotation from portrait to landscape by: 我试图通过以下方式强制界面从纵向旋转到横向:

  • setting a forceLandscape flag before attempting the rotation 在尝试旋转之前设置forceLandscape标志
  • Calling [UIViewController attemptRotationToDeviceOrientation] 调用[UIViewController attemptRotationToDeviceOrientation]
  • implementing -supportedInterfaceOrientations in my view controller and checking a flag to change the supported orientations, eg 在我的视图控制器中实现-supportedInterfaceOrientations并检查标志以更改支持的方向,例如

This forces -supportedInterfaceOrientations to be called again, which looks like 这会强制再次调用-supportedInterfaceOrientations ,看起来像

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

Even though -supportedInterfaceOrientations is being called and returns the new orientation, the interface does not rotate. 即使正在调用-supportedInterfaceOrientations并返回新方向,接口也不会旋转。 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. 我已在iOS 6和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;
}

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之前,界面不会旋转以匹配设备方向。

You could try to achieve something with: setStatusBarOrientation:animated: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/setStatusBarOrientation:animated : 您可以尝试使用以下内容实现:setStatusBarOrientation:animated: https//developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/ setStatusBarOrientation:animated

this fixed for me a similar issue. 这对我来说也是一个类似的问题。

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

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