一切都是按计划进行的。没有故事板和集合视图的vc和详细的vc都在TabBarController中。
我使用的是集合视图,当我点击didSelectItem
中的一个单元格时,我会按下一个详细的视图控制器。在DetailedVC中,我隐藏了导航控制器。我在viewDidLoad
和viewWillAppear
中单独和累积地调用了下面的代码,试图隐藏它:
navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)
当场景第一次出现时,导航条是隐藏的。问题是,当我在DetailedVC上滑动时,导航栏从屏幕顶部向下滑动,它不会消失。我错误地发现了它。
我按下导航栏的后退按钮,即使它应该被隐藏,它也能工作。我之所以隐藏它,是因为我有一个在DetailedVC顶部播放的视频,所以我使用一个自定义按钮弹出集合视图。我也隐藏状态栏(类似于YouTube),但这是隐藏的。
DetailedVC是一个常规的视图控制器,它不包含表视图或集合视图,所以我不明白为什么它允许我向下滑动,为什么导航条不会隐藏?
将DetailedVC推上的集合视图单元格:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailVC = DetailController()
navigationController?.pushViewController(detailVC, animated: true)
}
DetailedVC:
class DetailController: UIViewController {
let customButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("< Back", for: .normal)
button.setTitleColor(UIColor.orange, for: .normal)
button.addTarget(self, action: #selector(handleCustomButton), for: .touchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.isStatusBarHidden = true
// I tried all of these individually and cumulatively and the nav still shows when I swipe down
navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.isStatusBarHidden = false
}
@objc fileprivate func handleCustomButton()
navigationController?.popViewController(animated: true)
}
@objc fileprivate func configureButtonAnchors()
//customButton.leftAnchor...
}
发布于 2018-03-08 20:51:00
我不太清楚为什么当我在DetailVC内部滑动时,导航栏变得没有隐藏,但是我移动代码以将它隐藏在viewDidLayoutSubviews
中,现在它仍然是隐藏的。
为了解决这个问题,我使用了navigationController?.setNavigationBarHidden(true, animated: false)
并将其设置在viewDidLayoutSubviews
中。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// this one worked the best
navigationController?.setNavigationBarHidden(true, animated: false)
}
并将其设置为可以在前面的vc中显示,这将是集合视图:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: false)
}
我说它是最好的,因为我分别尝试了所有的3个,而其中的3个navigationController?.navigationBar.isHidden = true
是有问题的。由于某些原因,即使在viewDidLayoutSubviews
中,即使导航栏没有重新出现,也会使DetailedVC猛然上下移动。
navigationController?.isNavigationBarHidden = true
在DetailedVC内部工作,导航条保持隐藏,场景没有抖动,但当我在viewWillDisappear
中将其设置为false时,导航条将显示在父vc (集合视图)中,导航条不会出现在其中。
https://stackoverflow.com/questions/49184714
复制相似问题