Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 371
Francesco Zaffaroni Avatar asked Jan 28 '26 17:01

Francesco Zaffaroni


1 Answers

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

like image 177
Andrea Mugnaini Avatar answered Jan 31 '26 10:01

Andrea Mugnaini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!