Search code examples
iosswiftuikitstatusbar

Animate status bar alongside custom transition in segue


I have two ViewControllers, using a custom segue from the first I present the second, with custom animator and a percent based interactor.

I want to slide up the status bar alongside the controller transition.

In the second ViewController I have this in order to hide the status bar

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }

override var prefersStatusBarHidden: Bool {
        return true
    }

I know I should call setNeedsStatusBarAppearanceUpdate() in an animation block in order to animate it, and use UIViewControllerTransitionCoordinator method animateAlongsideTransition: in order to make it follow the transition, but I am not sure where should I call this method.

I tried in the viewWillAppear method of the second controller but it still disappeared immediately without animation.

What is the right place to call this?


Solution

  • The reason why you couldn't see the animation is because in your second view controller you always return true to prefersStatusBarHidden, then such view controller starts with that condition, hence doesn't have any "chance" for playing the animation.

    So in the second view controller you might try doing so:

    class ViewController2: UIViewController {
      override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { get { return .slide } }
      override var prefersStatusBarHidden: Bool { return statusBarHidden }
      var statusBarHidden = false
    
      override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        self.statusBarHidden = true
        UIView.animate(withDuration: 0.35) {
          self.setNeedsStatusBarAppearanceUpdate()
        }
      }
    }
    

    Moreover in your Info.plist be sure to have View controller-based status bar appearance= YES