如何在 UINavigationController 中正确旋转视图控制器? [英] How to properly rotate viewcontrollers in UINavigationController?

查看:32
本文介绍了如何在 UINavigationController 中正确旋转视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINavigationController,它的视图嵌入到 UIViewController 中.当我旋转设备时,NavigationController 完美地接收了 willRotate didRotate 事件.但是,在 UINavigationController 内部当前可见视图控制器之前推送的任何 UIViewController 都不会接收旋转事件.

I have a UINavigationController which's view is embedded into UIViewController. When I rotate the device, NavigationController perfectly received the willRotate didRotate events. However, any UIViewControllers pushed before the current visible view controller inside UINavigationController does not receive rotation events.

问题来了:虽然第二个 viewController 在 navigationController 中可见,但如果我旋转设备,ViewController 会根据旋转更改它的布局.当我弹出视图控制器以返回到第一个视图控制器时,第一个视图控制器根据纵向布局显示自己,因为它不会为旋转视图调用我的重新布局方法.

Here is the problem: While the second viewController is visible in the navigationController, if I rotate the device, ViewController changes it's layout according to the rotation. When I pop the viewcontroller to get back to first view controller, first view controller shows itself according to portrait layout as It doesn't call my re-layouting methods for the rotated view.

我不想为旋转更改添加观察者,因为它通常已经正常运行,因为默认旋转方法会自动正常调用...

I don't want to add observers for rotation change because it normally already behaves correctly as default rotation methods are automatically called normally...

在这种情况下我该怎么办?

What can I do in this situation?

我是通过手动调用 willRotate 函数来完成的,但我觉得这不是很好.

I have done it by manually calling willRotate function but I don't feel that this is good.

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UIInterfaceOrientation io=[[UIApplication sharedApplication] statusBarOrientation];

    [self willRotateToInterfaceOrientation:io duration:0.3];

    for(int i=0;i<self.childViewControllers.count;i++){
        [[self.childViewControllers objectAtIndex:i]willRotateToInterfaceOrientation:io duration:0.3];
    } 
}

推荐答案

我认为您不需要任何旋转事件观察者.但是,UINavigationController 中的 UIViewController 实例需要收到有关布局更改的通知.如果您将 UINavigationController 添加到视图控制器层次结构中,这会自动发生:

I don't think you need any rotation event observers. However, the UIViewController instances in the UINavigationController need to be notified about layout changes. This happens automatically if you add your UINavigationController to the view controller hierarchy:

[yourParentViewController addChildViewController:yourNavigationController];

这将确保您的导航控制器收到有关布局更改的通知,并将此信息传递给其所有视图控制器.

This will make sure that your navigation controller gets notified about layout changes, and will pass this information on to ALL its view controllers.

然后你可以简单地在 UIViewControllerviewDidLayoutSubviews 方法(或者如果你有一个子类视图,在它的 layoutSubviews 方法.

Then you can simply do your layout in the UIViewController's viewDidLayoutSubviews method (or if you have a subclassed view, in its layoutSubviews method.

注意:此方法需要 iOS 5 或更高版本!

Note: This method requires iOS 5 or higher!

这篇关于如何在 UINavigationController 中正确旋转视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆