Everything is happening programmatically. No Storyboard and The collection view's vc and the detailed vc are both inside a TabBarController.
I'm using a collection view and when I tap a cell in didSelectItem
I push on a detailed view controller. In the DetailedVC I hide the navigation controller. I called the below in viewDidLoad
and in viewWillAppear
both individually and cumulatively to try and keep it hidden:
navigationController?.isNavigationBarHidden = true
navigationController?.navigationBar.isHidden = true
navigationController?.setNavigationBarHidden(true, animated: false)
When the scene first appears the nav bar is hidden. The problem is when I swipe down on the DetailedVC the navigation bar comes down from the top of the screen with a swipe and it doesn't disappear. I discovered it by swiping down by mistake.
I press the navigation bar's back button and it works even though it should be hidden. The reason I hide it is because I have a video that plays at the very top of the DetailedVC so I use a custom button to pop back to the collection view. I also hide the status bar (similar to YouTube) but that stays hidden.
The DetailedVC is a regular view controller and it doesn't contain a table view or collection view so I'm confused as to why it's letting me swipe down and why the navigation bar won't stay hidden?
The collection view cell that pushes the DetailedVC on :
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let detailVC = DetailController()
navigationController?.pushViewController(detailVC, animated: true)
}
The 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...
}
I'm not exactly sure why when I was swiping down while inside the DetailVC the navigation bar became unhidden but I moved the code to hide it in viewDidLayoutSubviews
and now it stays hidden.
To fix the problem I usednavigationController?.setNavigationBarHidden(true, animated: false)
and set it inside viewDidLayoutSubviews
:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// this one worked the best
navigationController?.setNavigationBarHidden(true, animated: false)
}
And to set it so it can show back inside the previous vc which would be the collection view:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: false)
}
I said it worked the best because I tried all 3 separately and of the 3 navigationController?.navigationBar.isHidden = true
was buggy. For some reason even in viewDidLayoutSubviews
it was making the DetailedVC jerk up and down even though the navigation bar didn't reappear.
And navigationController?.isNavigationBarHidden = true
worked inside the DetailedVC, the nav bar stayed hidden and the scene didn't jerk but when I set it to false in viewWillDisappear
so that the nav bar would show inside the parent vc (the collection view) the nav bar didn't appear there.