繁体   English   中英

在iOS6 / 7上未调用willAnimateRotationToInterfaceOrientation

[英]willAnimateRotationToInterfaceOrientation not called on ios6/7

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

在iOS 7中强制横向和自动旋转

iOS 6中的Autorotate行为异常

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

iOS 6自动旋转问题和帮助

这导致我执行以下操作:

我已经在我的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中。 我已经在我所有应用程序的UIViewControllers(包括上面提到的PreloadingViewController)的SuperViewController(继承父级)中重写了shouldAutorotatesupportedInterfaceOrientations

- (BOOL)shouldAutorotate{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

在每个子UIViewController中,我都覆盖

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

使用代码以理想的方式将ui元素布局为纵向和横向。

最后我的应用程序的plist文件在

支持的界面方向

包含:

纵向(底部主页按钮),风景(左侧主页按钮),风景(右侧主页按钮)

我要支持的所有方向。

不过,即使supportedInterfaceOrientationsshouldAutorotate正在呼吁对我的RootViewController的每一个方向变化, willAnimateRotationToInterfaceOrientation永远不会被调用。 我什至在SuperViewController中重写了shouldAutomaticallyForwardRotationMethods ,以返回YES,但无济于事。

我在这里做错了什么? 有任何想法吗? 我什至认为旧的ios5-样式的xibs会引起问题,但我认为情况并非如此。

提前致谢。

在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]));
    }
}

由于我的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,并将其推入其之上的连续UIViewControllers。 由于我不希望导航条在我的应用程序中可见,因此我将导航控制器的navigationBarHidden属性设置为YES。 这样,将在当前位于navigationCotroller的控制器堆栈顶部的每个UIViewController上调用方法willAnimateRotationToInterfaceOrientation ,并且其相应的视图和视图控件可针对所需方向正确旋转。

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

实现此方法,根据需要返回YES。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

暂无
暂无

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

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