Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

swift - Hide statusbar with overCurrentContext presentation style and custom transition

I have a UITableViewController embedded in a UINavigationController. If the user taps on a cell, I present a new ViewController (DetailViewController) with modalPresentationStyle= .overCurrentContext and a custom transitioningDelegate:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // ...
    let detailVC = storyboard?.instantiateViewController(identifier: "detailView") as! DetailViewController
    detailVC.modalPresentationStyle = .overCurrentContext
    detailVC.modalPresentationCapturesStatusBarAppearance = true
    detailVC.transitioningDelegate = transitionManager
    present(detailVC, animated: true, completion: nil)
}

In the DetailViewController I overrode two properties:

override var prefersStatusBarHidden: Bool {
    return true
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
    return .slide
}

The problem is that the statusbar does not hide. It stays visible. Changing .overCurrentContext to .overFullScreen works, but I want to/have to use .overCurrentContext. Do I have to implement something in the transitioningDelegate? I only want to hide the statusbar in this specific view controller.

Thanks in advance

EDIT: Partly working solution

I was able to solve it. At least partly. Somehow it only worked if I created a Subclass for the UINavigationController and use this class instead of the default when creating it with my TableViewController as rootViewController:

class CustomNavController : UINavigationController {
    var isStatusBarHidden = false {
        didSet {
           setNeedsStatusBarAppearanceUpdate()
        }
    }
    override var prefersStatusBarHidden: Bool {
        return isStatusBarHidden
    }
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
        return .slide
    }
}

In my DetailViewController I had to get a reference to the parent TableViewController. This way I was able to write following code in viewWillAppear and viewWillDisappear:

override func viewWillAppear(_ animated: Bool) {
    if let my = self.parentTableViewController.navigationController as? CustomNavController {
       my.isStatusBarHidden = true
    }
}

override func viewWillDisappear(_ animated: Bool) {
    if let my = self.parentTableViewController.navigationController as? CustomNavController {
        my.isStatusBarHidden = false
    }
}

What doesn't work is the animation. The statusbar just disappears instead of sliding away.

question from:https://stackoverflow.com/questions/65858119/hide-statusbar-with-overcurrentcontext-presentation-style-and-custom-transition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...