2017-07-12 217 views
0

主要有UIWindow,它包含MainViewController,它使用lightContent作爲preferredStatusBarStyle。我創建了第二個UIWindow實例來顯示PopupViewController,它使用default作爲preferredStatusBarStyle關閉第二個UI窗口後恢復狀態欄狀態

當我表明第二UIWindowPopupViewController狀態欄樣式更改default,但是當我把它隱藏樣式不更改回lightContent

同樣的問題適用於當我在彈出窗口中隱藏狀態欄的情況下使用VC的情況 - 當彈出窗口被解除時狀態欄不顯示。

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() 

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() 
    } 
) 

謝謝!

編輯:彈出窗口可以出現在任何時間,我不知道哪個VC是活躍在那一刻

回答

0

我一直在尋找的東西是UIViewController.setNeedsStatusBarAppearanceUpdate()。告訴VC狀態欄外觀已更改並需要恢復是一種方便的方法。

// 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

謝謝大家!

0

變化對viewWillAppear中的狀態欄風格

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

Popup可以出現在隨機的VC上。手動設置樣式對我來說感覺很不好。我認爲有一些方法可以告訴「好的,現在這個窗口再次成爲主人,這裏是rootVC,繼承它的狀態欄設置」。 –

+0

您是否在「info.plist – Ragul

+0

」中將「基於控制器的狀態欄外觀」設置爲NO?不幸的是,我需要它基於VC的 –

0
  1. 請添加在您的第一個控制器上的代碼更改狀態欄顏色Light。

    覆蓋FUNC viewDidAppear(_動畫:BOOL){UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent,動畫:假)}

  2. 現在要在其中默認的狀態欄樣式第二控制器,把代碼:

    覆蓋FUNC viewDidAppear(_動畫:BOOL){UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default,動畫:假)}

你一定會得到通過上述C妥善解決對於你的情況頌歌

+0

問題是popup popup出現在任何時候,我現在不是哪個VC在那個時候活躍 –

0

簡單的解決方案

  1. 顯示窗口

爲你的情況

  1. 新的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. 新的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

這很好,因爲不像其他建議的解決方案,這個處理一切正確的彈出VC,但看起來像'UIApplication.shared.setStatusBarStyle()'從9.0 –

+0

@Andrejs Mironovs'UIApplication.shared.statusBarStyle = .lightContent'我已經在iOS 10中工作,此代碼工作正常,請檢查它。 – Vivek