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

ale*_*ran 6 iphone cocoa-touch rotation uiviewcontroller ios

根据UIViewController这里的文档,

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

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

  • forceLandscape在尝试旋转之前设置标志
  • 调用 [UIViewController attemptRotationToDeviceOrientation]
  • 落实-supportedInterfaceOrientations在我的视图控制器和检查标志,以改变支撑取向,如

这种力量-supportedInterfaceOrientations再次被召唤,看起来像

- (NSUInteger)supportedInterfaceOrientations {
    return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

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

以下是重现的完整代码:

@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;
}
Run Code Online (Sandbox Code Playgroud)

bil*_*tum 10

调用attemptRotationToDeviceOrientation仅旋转接口的方向,当且仅当设备本身已旋转时.

例如,如果视图控制器仅支持纵向方向,并且用户将设备旋转到横向,则不会发生界面旋转.当设备处于横向时,假设您的代码切换了一个允许横向定位的标志.接口方向会发生什么?没什么,因为已经发生了设备方向.要使接口方向与设备方向匹配,您需要调用attemptRotationToDeviceOrientation.

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

  • 我开始同意你的看法.但是,文档建议我们可以根据应用程序的具体情况更改支持的接口:"某些视图控制器可能希望使用特定于应用程序的条件来确定支持哪些接口方向.如果视图控制器执行此操作,那么这些条件更改,您的应用应该调用此类方法.系统会立即尝试旋转到新方向." (2认同)
  • 我在这里问了一个类似的问题 http://stackoverflow.com/questions/20987249/how-do-i-programmatically-set-device-orientation-in-ios7 我想我的问题是 tryRotationToDeviceOrientation 如果没有,它如何有用实际上旋转?这曾经在 iOS6 之前是可能的,带有 [UIDevice currentDevice] setOrientation。这是一个在 iOS7 中使用编译器警告的 hack,但我不会被 App Store 提交接受:`objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);` (2认同)