web-dev-qa-db-fra.com

"viewWillTransitionToSize:" non appelé dans iOS 9 lorsque le contrôleur de vue est présenté sous forme modale

Je présente un contrôleur de vue d'un autre:

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

Ensuite, dans la UIViewController présentée, la méthode viewWillTransitionToSize:withTransitionCoordinator: est appelée dans iOS 8 mais pas dans iOS 9...

Merci

18
AppsDev

Dans votre contrôleur de vue actuel, si vous remplacez viewWillTransitionToSize:withTransitionCoordinator:, veillez à appeler super. Sinon, ce message ne sera pas propagé aux enfants, voir les contrôleurs. 

Pour Objective-C :

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

    // Your other code ... 

Et Swift :

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

    // Your other code ...
}
23
Yuchen Zhong

Cela peut sembler un cas trivial, mais lorsque vous utilisez un iPad, vérifiez que l'utilisateur n'a pas activé le verrouillage de la rotation dans les paramètres, le panneau de configuration ou le bouton latéral.

2
Steve Brooker

Peut-être qu'il est un peu tard, mais j'ai mis ceci ici pour quiconque tombe sur ce problème frustrant. 

Gardez à l'esprit que viewWillTransitionToSize:withTransitionCoordinator: est parfois appelé sur la presentingViewController du contrôleur de vue auquel vous vous attendez. (Et si ce contrôleur de vue a aussi une presentingViewController, il peut être appelé à ce sujet)

Je ne pouvais pas vraiment comprendre la logique derrière tout ça, mais c'était le but de mon cas. J'ai donc dû remplacer viewWillTransitionToSize:withTransitionCoordinator: dans beaucoup de mes contrôleurs de vue comme ceci:

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

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

Les gens ont déjà expliqué que vous devez appeler super . J'aimerais ajouter un élément d'information qui pourrait aider les personnes qui auraient été confrontées à ce que j'ai été confronté. 

Scénario: Parent -> Enfant (viewWillTransition non appelé dans l'enfant)


Si votre contrôleur de vue est un contrôleur de vue enfant, vérifiez si le délégué parent contrôleur de vue est appelé et si super est appelé à cet endroit. Sinon, il ne sera pas propagé au contrôleur de vue enfant!

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 - Cela m'est arrivé parce que je n'ai pas écrit le code pour le parent.

0
Rakesha Shastri