视图控制器以模态方式显示时,未在iOS 9中调用“viewWillTransitionToSize:”

时间:2015-10-01 09:34:05

标签: ios uiviewcontroller screen-orientation autorotate presentviewcontroller

我提出另一个视图控制器:

- (void)showModalView
{
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   MySecViewController *mySecViewController = [storyboard instantiateViewControllerWithIdentifier:@"secController"];
   mySecViewController.delegate = self;
   [self presentViewController:mySecViewController animated:YES completion:nil];
}

然后在提交的UIViewController中,方法viewWillTransitionToSize:withTransitionCoordinator:iOS 8中调用,但不在iOS 9中调用...

由于

4 个答案:

答案 0 :(得分:24)

在您当前的视图控制器中,如果您覆盖viewWillTransitionToSize:withTransitionCoordinator:,请务必致电super。否则,此消息将不会传播到子视图控制器。

Objective-C

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Your other code ... 

Swift

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    // Your other code ...
}

答案 1 :(得分:2)

看起来似乎很简单,但在使用iPad时检查用户是否未在设置或控制面板或侧面按钮中激活旋转锁定

答案 2 :(得分:1)

也许它有点晚了,但是我把它放在这里,因为任何人都会遇到这个令人沮丧的问题。

请记住,viewWillTransitionToSize:withTransitionCoordinator:有时会在您期望的视图控制器的presentingViewController上调用。 (如果该视图控制器也有presentingViewController,则可能会调用它)

我无法弄清楚这背后的逻辑,但这就是我的观点。所以我不得不在我的许多视图控制器中覆盖viewWillTransitionToSize:withTransitionCoordinator:,如下所示:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    [self.presentedViewController viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

答案 3 :(得分:1)

人们已经解释过,您必须致电 super 。我想补充一条信息,可能会对那些会面对我的人有所帮助。

方案:父级->子级(未在子级中调用viewWillTransition)


如果您的视图控制器是 child 视图控制器,则检查是否调用了 parent 视图控制器委托,以及是否在其中调用了 super 。否则它将不会传播到子视图控制器!

class ParentViewController: UIViewController {

    func presentChild() {
        let child = ChildViewController()
        present(child, animated: false, compeltion: nil)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator) // If this line is missing your child will not get the delegate call in it's viewWillTransition

        // Do something
    }
}

class ChildViewController: UIViewController {

    // This method will not get called if presented from parent view controller and super is not called inside the viewViewWillTransition available there.
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
       super.viewWillTransition(to: size, with: coordinator)

       //Do something
    }
}

P.S-这发生在我身上,因为我没有为父母写代码。