2016-05-28 1 views
2

を無視されて、私はそうのようなMainViewControllerを提示しているAuthViewControllerを持っている:preferredStatusBarUpdateAnimationが

let mainVC = MainViewContoller() 
mainVC.modalTransitionStyle = .CrossDissolve 
authVC.presentViewController(mainVC, animated: true, completion: nil) 

ので、同じように、私はAuthViewControllerは、ステータスバーを非表示にしたいのですが、MainViewControllerを表示するには:

AuthViewController { 

    override func preferredStatusBarStyle() -> UIStatusBarStyle { 
     return .LightContent 
    } 

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation { 
     return .Fade 
    } 

    override func prefersStatusBarHidden() -> Bool { 
     return false 
    } 
} 

MainViewController { 

    override func preferredStatusBarStyle() -> UIStatusBarStyle { 
     return .LightContent 
    } 

    override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation { 
     return .Fade 
    } 

    override func prefersStatusBarHidden() -> Bool { 
     return false 
    } 
} 

ステータスバーが表示されますが、preferredStatusBarUpdateAnimation()のオーバーライドは無視されます。ステータスバーにアニメーションが表示されません。

私はそれがその後、これを呼び出して、viewDidAppearまでtrueMainViewControllerprefersStatusBarHiddenを設定することにより、アニメーション化するために取得することができました:

UIView.animateWithDuration(0.3) { 
    self.setNeedsStatusBarAppearanceUpdate() 
} 

私は、これは毎回呼び出すする必要がありますする必要はありません。私は間違って何をしていますか?

答えて

2

これについても解決策を探していました。しかし、あなたは既に解決策の半分を持っているようです。変数のオーバーライドを設定する必要がありますが、それぞれのオーバーライドに対してプライベート変数のペアを作成し、それぞれのプライベート変数が返されるようにするのがベストプラクティスです(以下を参照)。プライベート変数を変更し、setNeedsStatusBarAppearanceUpdateに電話してください。

さらに、info.plistYESView controller-based status bar appearanceを設定する必要があります。さもなければ、それは依然として従来のUIApplication.shared...メソッドに依存します。

がここに抜粋です:

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { 
    get { 
     return .slide 
    } 
} 

private var statusBarStyle : UIStatusBarStyle = .default 

override var preferredStatusBarStyle: UIStatusBarStyle { 
    get { 
     return statusBarStyle 
    } 
} 

private var statusBarStatus : Bool = false 

override var prefersStatusBarHidden: Bool { 
    get { 
     return statusBarStatus 
    } 
} 

私は、その後のような関数で呼び出すことができますので:(これは私の例の一つであるので、カスタム関数を無視します)。

func sliderView(sliderView: SliderView, didSlideToPlace: CGFloat, within: CGFloat) { 

    let val = (within - (didSlideToPlace - sliderView.upCent))/(within) 
    print(val) 
    //Where you would change the private variable for the color, for example. 
    if val > 0.5 { 
     statusBarStyle = .lightContent 
    } else { 
     statusBarStyle = .default 
    } 
    UIView.animate(withDuration: 0.5, animations: { 
     sliderView.top.backgroundColor = UIColor.black.withAlphaComponent(val) 
     self.coverLayer.alpha = val 
     self.scroll.backgroundColor = colors.lightBlueMainColor.withAlphaComponent(val) 
    }, completion: { 
     value in 
     //If you do not call setNeedsStatusBarAppearanceUpdate() in an animation block, the animation variable won't be called it seems. 
     UIView.animate(withDuration: 0.4, animations: { 

      self.animating = true 

      //Where you set the status for the bar (your part of the solution) 
      self.statusBarStatus = false 

      //Then you call for the refresh 
      self.setNeedsStatusBarAppearanceUpdate() 
     }) 
    }) 
} 

あなたはすべてのビューコントローラチューンを有する終わるが、それはあなたが何をしたいのですように思えるので、それが役に立てば幸い!

関連する問題