一切都以编程方式发生。没有情节提要和集合视图的 vc 和详细的 vc 都在 TabBar 控制器中。
我正在使用集合视图,当我点击didSelectItem
中的单元格时,我会推送详细的视图控制器。在详细VC中,我隐藏了导航控制器。 我在viewDidLoad
和viewWillAppear
中单独和累积地调用了以下内容,以尝试将其隐藏起来:
navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)
当场景首次出现时,导航栏处于隐藏状态。问题是当我在 DetailedVC 上向下滑动时,导航栏通过滑动从屏幕顶部下来,它不会消失。我通过错误地向下滑动发现了它。
我按下导航栏的后退按钮,即使它应该被隐藏,它也可以工作。我隐藏它的原因是因为我有一个视频在 DetailedVC 的最顶部播放,所以我使用自定义按钮弹出回集合视图。我还隐藏了状态栏(类似于YouTube),但仍然隐藏。
DetailedVC是一个常规的视图控制器,它不包含表视图或集合视图,所以我很困惑为什么它让我向下滑动以及为什么导航栏不会保持隐藏状态?
将详细 VC 推送到的集合视图单元格:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailVC = DetailController()
navigationController?.pushViewController(detailVC, animated: true)
}
详细VC:
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...
}
我不确定为什么当我在 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(集合视图)内导航栏没有出现在那里。