gpt4 book ai didi

ios - UINavigationBar 滑开而不是停留在原地

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:33 24 4
gpt4 key购买 nike

我创建了演示项目来展示问题。

我们在 UINavigationController 中有两个 View Controller 。

MainViewController 是根。

class MainViewController: UIViewController {

lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Detail", for: .normal)
return button
}()

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Main"
view.backgroundColor = .blue
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 150).isActive = true
button.heightAnchor.constraint(equalToConstant: 42).isActive = true
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
}

@objc func buttonTapped(_ sender: UIButton) {
navigationController?.pushViewController(DetailViewController(), animated: true)
}
}

和被推送的DetailViewController

class DetailViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
}

如您所见,我想在 DetailViewController 中隐藏 UINavigationBar:

Demo

问题

问题是,UINavigationBar 会滑走,而不是与整个 MainViewController 一起留在原处。我怎样才能改变这种行为并保持弹出手势?

最佳答案

在您的 MainViewController 中添加该方法

override func viewDidAppear(_ animated: Bool) {        
UIView.animate(withDuration: 0) {
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
}

并在 DetailViewController 中用以下方法替换您的方法

 override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}

关于ios - UINavigationBar 滑开而不是停留在原地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47223164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com