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

App*_*Dev 20 uiviewcontroller screen-orientation autorotate ios 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];
}
Run Code Online (Sandbox Code Playgroud)

然后在呈现中UIViewController,该方法viewWillTransitionToSize:withTransitionCoordinator:被调用iOS 8但不在iOS 9...

谢谢

Yuc*_*ong 24

在当前视图控制器中,如果覆盖viewWillTransitionToSize:withTransitionCoordinator:,请确保调用super.否则,此消息将不会传播到子视图控制器.

对于Objective-C:

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

    // Your other code ... 
Run Code Online (Sandbox Code Playgroud)

斯威夫特:

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

    // Your other code ...
}
Run Code Online (Sandbox Code Playgroud)


Rak*_*tri 5

人们已经解释过你必须调用super。我想添加一条信息,可能会帮助那些会遇到我所面临的情况的人。

场景:父级 -> 子级(viewWillTransition 未在子级中调用)


如果您的视图控制器是视图控制器,则检查是否调用了视图控制器委托以及是否在那里调用了 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
    }
}
Run Code Online (Sandbox Code Playgroud)

PS - 这发生在我身上,因为父代码是由其他人编写的,他们忘记调用 super。