2017-01-09 67 views
0

我有兩個視圖控制器。 MainViewController和SecondViewController(這個嵌入在導航控制器中)。 MainViewController有一個UIButton,它將以模態方式呈現SecondViewController,而SecondViewController具有一個將關閉自身的UIButton。prefersStatusBar隱藏幻燈片動畫不能在設備上工作

他們每個人有以下代碼:

var statusBarHidden = false { 
    didSet { 
     UIView.animate(withDuration: 0.5) {() -> Void in 
      self.setNeedsStatusBarAppearanceUpdate() 
     } 
    } 
} 

override var prefersStatusBarHidden: Bool { 
    return statusBarHidden 
} 

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { 
    return .slide 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    statusBarHidden = true 
} 

狀態欄的幻燈片動畫作品在模擬器偉大的,但不是實際的設備上,我究竟做錯了什麼?

我使用xCode 8.2.1和Swift 3

回答

0

我最終做的是這樣的。我創建了一個鏈接到狀態欄視圖並添加了功能的變量,以便我可以做我需要的。

extension UIApplication { 
    var statusBarView: UIView? { 
     return value(forKey: "statusBar") as? UIView 
    } 

    func changeStatusBar(alpha: CGFloat) { 
     statusBarView?.alpha = alpha 
    } 

    func hideStatusBar() { 
     UIView.animate(withDuration: 0.3) { 
      self.statusBarView?.alpha = 0 
     } 
    } 

    func showStatusBar() { 
     UIView.animate(withDuration: 0.3) { 
      self.statusBarView?.alpha = 1 
     } 
    } 
} 

一個典型的應用是:

func scrollViewDidScroll(_ scrollView: UIScrollView) { 

    let alpha = tableView.contentOffset.y/100 
    UIApplication.shared.changeStatusBar(alpha: alpha) 

}