ios - iPad 应用程序方向和重新定位问题

标签 ios xcode ipad orientation uimodalpresentationstyle

当我的 UI 流程如下时,我的应用程序中出现了一个奇怪的错误:

  1. mainViewController 以纵向启动,然后显示一个 LoginViewController,其 modalPresentationStyle 设置为 UIModalPresentationFullScreen

  2. 我将 loginViewController 切换为横向模式,输入凭据进行登录(验证用户的方法在 mainViewController 中定义)。

  3. loginViewController 被关闭(从 mainViewController 中)并且屏幕上加载了一堆按钮以指示它是主屏幕。

现在我的问题是,即使在显示 loginViewController 时应用程序切换到横向,按钮位置看起来就像应用程序处于纵向方向一样。

此外,只有当 modalPresentationStyle 设置为 UIModalPresentationFullScreen 时才会发生这种情况!当我将它显示为表单或页面时,一切正常(但是,我需要我的 loginViewController 全屏显示)。

到目前为止,我已经尝试在 loginViewController 被关闭等情况下手动调用 shouldAutorotate 方法,这解决了问题,但似乎是一个劣质的解决方法,而不是修复。

我也试过从 loginViewController 调用 mainViewController 的 shouldAutorotate 方法,但这没有任何改变。

关于如何解决这个问题有什么想法吗?

最佳答案

Autorotation 在 iOS 6 中发生了变化。在 iOS 6 中,UIViewController 的 shouldAutorotateToInterfaceOrientation: 方法已弃用。取而代之的是,您应该使用 supportedInterfaceOrientations:shouldAutorotate 方法:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

Modal ViewControllers 在 iOS 6 中不再获得旋转调用:willRotateToInterfaceOrientation:duration:、willAnimateRotationToInterfaceOrientation:duration:didRotateFromInterfaceOrientation: 方法不再在任何对自身进行全屏显示的 View Controller 上调用——例如那些调用的 View Controller :presentViewController:animated:completion:

This answer will further explain the rotation changes in iOS 6


[编辑] 完全不同的方式是注册轮换事件。优点是所有对象都可以为此注册,而不仅仅是UIViewController。这通常在 View 加载时完成,并在 View 消失时停止(在此处放置 dealloc)。选择器中的方法在方向改变时被调用(这里是orientationChanged:):

- (void)viewDidLoad {
    [super viewDidLoad];
    // Start generating device rotations and register for them
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc {
    // Deregister and stop generating device rotations
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [super dealloc];
}

关于ios - iPad 应用程序方向和重新定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13740160/

相关文章:

objective-c - 模态视图 Controller 转换

objective-c - iOS + jqueryMobile 无法在加载 html 文件时捕获 webview 事件

iphone - ARC 在 block 内捕获 self... block 并在执行前释放引用

objective-c - 检测 UITextfield 中文本的更改

javascript - 通过javascript检测ipad/iphone webview

ios - 从 super View iOS 中删除 View

xcode - 在 Swift 中打开 NSMenu 时 NSTimer 不触发

ios - Xcode:在 Instruments 中未发现僵尸

ios - 从另一个应用程序打开 Safari 的深层链接

ios - 在 CCSprite 中淡入淡出图像?