Hide status bar only on modally represented view controller

I have a view controller Athat shows the status bar on top. From this view controller, I want to present another view controller Bthat hides the status bar. To achieve this, I redefine the property

override var prefersStatusBarHidden: Bool {
    return true
}

on B. To ensure smooth animation whenever the status bar (dis) appears, I also override the property

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
}

However, when I now present the view controller Bfrom A, the status bar disappears abruptly and Ais still visible, right before the start of the animated modal transition.

I am looking for a way to fix this "transition status bar" behavior . Ideally, I would like to have a clean separation:

  • A: shows the status bar
  • B: does not show the status bar

so when i present the Bstatus bar is covered by it.

Since the status bar appears to be a global view that does not belong to any particular controller, it is probably difficult for it to achieve this behavior. Therefore, if it is not possible to reproduce this exact behavior of the animation, I would also be glad if the status bar would slide smoothly during the transition of the view controller. How can i achieve this?

+4
1

- B:

var willAppear = false

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
}

override var prefersStatusBarHidden: Bool {
    return willAppear
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    willAppear = true
    UIView.animate(withDuration: 0.5) {
        self.setNeedsStatusBarAppearanceUpdate()
    }
}

, , , , .

, , , viewWillAppear .

EDIT:

"" - ( A):

var willAppear = false

override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
}

override var prefersStatusBarHidden: Bool {
    return willAppear
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if let _ = presentedViewController as? B {
        willAppear = true
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    if let _ = presentedViewController as? B {
        willAppear = false
        UIView.animate(withDuration: 0.5) {
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }
}

, -, , .

+4

Source: https://habr.com/ru/post/1679073/


All Articles