【问题标题】:willAnimateRotationToInterfaceOrientation not called on ios6/7ios6/7 上未调用 willAnimateRotationToInterfaceOrientation
【发布时间】:2013-12-23 21:43:24
【问题描述】:

我有一个旧应用程序,它仍然存在于 iTunes 上,用 iOS 5 编写。我想更新它以在 ios 6 和 7 上运行。到目前为止一切都很好,我已经更新了我的代码以使用 ARC .然而,当我试图保持同样的自转理念时,我总是碰壁。我已经检查了 SO 中的相关主题,例如:

Forcing landscape and autorotate in iOS 7,

Autorotate in iOS 6 has strange behaviour

并遵循一个类似的主题,我发现了这个:

iOS 6 Autorotation Problems and Help

这导致我做了以下事情:

我已经在我的 AppDelegate 中设置了 rootViewController,如下所示:

self.preloadingViewController = [[PreloadingViewController alloc] initWithNibName:@"PreloadingViewController" bundle:nil];
self.window.rootViewController = self.preloadingViewController;

我已放置:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAllButUpsideDown;

}

在我的 AppDelegate 中。我已经在我的所有应用的 UIViewController(包括上面提到的 PreloadingViewController)的 SuperViewController(继承术语中的父级)中覆盖了 shouldAutorotatesupportedInterfaceOrientations

- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

在每个子 UIViewController 中,我重写了

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

使用代码以适合纵向和横向方向的方式布局 ui 元素。

最后是我应用的plist文件在

支持的界面方向

包含:

纵向(底部主页按钮)、横向(左侧主页按钮)、横向 (右主页按钮)

我想要支持的所有方向。

尽管如此,即使在我的 rootViewController 上的每个方向更改都会调用 supportedInterfaceOrientationsshouldAutorotate,但永远不会调用 willAnimateRotationToInterfaceOrientation。我什至在我的 SuperViewController 中覆盖了 shouldAutomaticallyForwardRotationMethods 以返回 YES,但无济于事。

我在这里做错了什么?有任何想法吗?我什至认为旧的 ios5 - 风格的 xib 会导致问题,但我认为情况并非如此。

提前致谢。

【问题讨论】:

  • 你的viewcontroller可见吗?
  • willAnimateRotationToInterfaceOrientation: 不再为不可见的视图控制器调用方法。因此,如果您的视图控制器呈现另一个接管,则只有另一个会听到这些通知。
  • rootViewController 不可见。尽管我注意到只有 rootViewController 会收到有关方向更改的通知,但它的 willAnimateRotationToInterfaceOrientation 永远不会被调用。

标签: ios iphone objective-c uiviewcontroller autorotate


【解决方案1】:

在 iOS 6 中,willAnimateRotationToInterfaceOrientation 的调用方式发生了变化。

用途:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    // Something
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    // Something
}

在你的rootController

编辑:

新的 main.m

#import <UIKit/UIKit.h>
#import "yourAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([yourAppDelegate class]));
    }
}

【讨论】:

  • 您能解释一下 willAnimateRotationToInterfaceOrientation 的调用方式发生了变化吗?我已经在我的 rootController 中添加了您提出的方法,但它们仍然没有被调用。
  • 应该调用这些方法...让我想想...我在过去将项目从旧版本迁移到新版本时遇到了一些问题。你的 main.m 文件和我在 EDIT 中写的一样吗?
  • 我的 main.m 看起来不像您的编辑。我确实使用您提出的更改对其进行了更新,但没有实际效果。还是谢谢。
【解决方案2】:

实现这个方法,你需要的返回YES。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

【讨论】:

  • 此方法在 ios6 及更高版本中已弃用。我已经实现它以支持 iOS5 及更早版本。
【解决方案3】:

由于我的 rootViewController 是唯一一个调用其 shouldAutorotatesupportedInterfaceOrientations 方法的控制器,因此我决定注册所有其他视图控制器以获取任何 UIDeviceOrientationDidChangeNotification 的通知。

[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

- (void)orientationChanged:(NSNotification *)notification
{
    NSLog(@"Orientation changed!");
    [self layoutForOrientation:[[UIDevice currentDevice] orientation]];
}

在我的layoutForOrientation: 方法中,我处理了uiview 的控件方向。

但是,虽然我确实收到了UIDeviceOrientationDidChangeNotification 通知,但我的视图方向实际上并不会改变以匹配当前方向,即如果说新方向为UIInterfaceOrientationLandscapeRight,则视图方向将保持在UIInterfaceOrientationPortrait 和它的子视图将旋转 90 度。这肯定看起来不对劲。拔了很多头发后,我决定放弃这条路线。

相反,我将 rootViewController 设置为 UINavigationController,并让它在其上推送连续的 UIViewController。由于我不希望在我的应用程序中显示导航栏,我已将导航控制器的 navigationBarHidden 属性设置为 YES。这样,方法willAnimateRotationToInterfaceOrientation 会在当前位于navigationCoroller 控制器堆栈顶部的每个UIViewController 上被调用,并且其对应的视图和视图控件会针对所需方向正确旋转。

这是因为我的大多数视图控制器支持的界面方向都与掩码匹配:UIInterfaceOrientationMaskAllButUpsideDown,这是 iPhone 的默认行为。如果我需要支持不同的方向,我必须继承 UINavigationController 并覆盖 supportedInterfaceOrientationsshouldAutorotate 以启用导航堆栈的顶视图控制器所需的方向支持,根据 Apple 的示例项目:AlternateViews

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    相关资源
    最近更新 更多