2017-07-12 3 views
0

Il est le principal UIWindow qui détient MainViewController qui utilise lightContent comme preferredStatusBarStyle. J'ai créé la deuxième instance UIWindow pour afficher PopupViewController, qui utilise default comme preferredStatusBarStyle.Restauration de l'état de la barre d'état après la fermeture de la deuxième UIWindow

Quand je montre deuxième UIWindow avec PopupViewController état des changements de style bar à default, mais quand je le cache style ne pas revenir à des changements lightContent.

Le même problème s'applique à la situation où j'ai VC avec barre d'état masquée dans la fenêtre contextuelle - la barre d'état ne s'affiche pas lorsque la fenêtre contextuelle est fermée.

Nouvelle UIWindow création:

// Prepare window to show dialog box in 
newWindow = UIWindow(frame: UIScreen.main.bounds) 
newWindow?.windowLevel = 3 

// Overlay new window 
newWindow?.makeKeyAndVisible() 
self.mainWindow.windowLevel = 1 
self.mainWindow.endEditing(true) 
newWindow?.isHidden = false 

// Display dialog 
newWindow?.rootViewController = PopupViewController() 

licenciement Nouveau UIWindow:

UIView.animate(
    withDuration: 1.0, 
    delay: 0, 
    usingSpringWithDamping: 1, 
    initialSpringVelocity: 0, 
    options: .curveEaseOut, 
    animations: { [weak self] in 
     self?.newWindow?.alpha = 0 
    }, 
    completion: { [weak self] _ in 
     self?.newWindow?.windowLevel = 0 
     self?.newWindow?.rootViewController = nil 
     self?.newWindow?.alpha = 1 
     self?.mainWindow.makeKeyAndVisible() 
    } 
) 

Merci!

EDIT: Popup peut apparaître à tout moment, je ne sais pas quelle VC était actif à ce moment

Répondre

0

La chose que je cherchais était UIViewController.setNeedsStatusBarAppearanceUpdate().Il est pratique de dire à VC que l'apparence de la barre d'état a été modifiée et doit être restaurée.

// make main window key but transparent 
self.mainWindow.alpha = 0 
self.newWindow?.windowLevel = 0 
self.newWindow?.alpha = 1 
self.mainWindow.makeKey() 

// restore status bar appearance 
self.mainWindow.rootViewController!.setNeedsStatusBarAppearanceUpdate() 

// Fade in main window with (status bar is in proper state at this moment) 
UIView.animate(
     withDuration: 0.9, 
     delay: 0, 
     usingSpringWithDamping: 1, 
     initialSpringVelocity: 0, 
     options: .curveEaseIn, 
     animations: { [weak self] in 
      self?.mainWindow.alpha = 1 
     }, 
     completion: { [weak self] _ in 
      // destroy popup VC 
      self?.newWindow?.rootViewController = nil 
     } 
) 

Here is useful article on this subject

Merci à tous! Popup peut apparaître sur un VC aléatoire.

0

changer le style statusbar sur viewWillAppear

override func viewWillAppear(_ animated: Bool) { 
    UIApplication.shared.statusBarStyle = .lightContent 
} 
+0

Cela me semble un peu hacky de remettre le style manuellement. Je pensais qu'il y avait un moyen de dire "OK, maintenant cette fenêtre est à nouveau maître, ici c'est rootVC, hériter des paramètres de la barre d'état". –

+0

avez-vous réglé "Afficher l'apparence de la barre d'état basée sur le contrôleur" sur NON sur info.plist – Ragul

+0

Non, malheureusement, j'ai besoin de –

0
  1. S'il vous plaît ajouter le code suivant sur votre premier contrôleur dans lequel vous changez la couleur de la barre d'état Lumière.

    override func viewDidAppear (_ animé: Bool) {UIApplication.shared.setStatusBarStyle (UIStatusBarStyle.lightContent, animé: false)}

  2. maintenant en deuxième contrôleur dans lequel vous souhaitez le statut par défaut style bar, mettez le code :

    override func viewDidAppear (_ animé: Bool) {UIApplication.shared.setStatusBarStyle (UIStatusBarStyle.default, animé: false)}

Vous aurez certainement la bonne solution par-dessus c ode

+0

Problème est que popup peut apparaît à tout moment et je ne sais pas à présent quel VC était actif à ce moment-là –

0

solution simple pour votre cas

  1. Étalage

solution simple pour votre cas

  1. Nouvelle création UIWindow:

    // Prepare window to show dialog box in 
    newWindow = UIWindow(frame: UIScreen.main.bounds) 
    newWindow?.windowLevel = 3 
    
    // Overlay new window 
    newWindow?.makeKeyAndVisible() 
    self.mainWindow.windowLevel = 1 
    self.mainWindow.endEditing(true) 
    newWindow?.isHidden = false 
    
    // Display dialog 
    newWindow?.rootViewController = PopupViewController() 
    // Now you can change status bar style default or other style 
    UIApplication.shared.statusBarStyle = .default 
    
  2. licenciement Nouveau UIWindow:

    UIView.animate(
        withDuration: 1.0, 
        delay: 0, 
        usingSpringWithDamping: 1, 
        initialSpringVelocity: 0, 
        options: .curveEaseOut, 
        animations: { [weak self] in 
         self?.newWindow?.alpha = 0 
         // Revert back to default 
         UIApplication.shared.statusBarStyle = .lightContent 
        }, 
        completion: { [weak self] _ in 
         self?.newWindow?.windowLevel = 0 
         self?.newWindow?.rootViewController = nil 
         self?.newWindow?.alpha = 1 
         self?.mainWindow.makeKeyAndVisible() 
        } 
    ) 
    
+0

C'est bien parce que contrairement à d'autres solutions suggérées, tout se passe bien dans le popup VC, mais ressemble à 'UIApplication.shared.setStatusBarStyle()' est obsolète depuis 9.0 –

+0

@Andrejs Mironovs 'UIApplication.shared.statusBarStyle = .lightContent' J'ai déjà travaillé dans iOS 10 ce code fonctionne bien s'il vous plaît vérifier. – Vivek