hidesBarsOnSwipe method not working when swiping my collection view - ios

I tried implementing the simple one line of code shown below into my viewDidLoad in order to have my navigaion bar hide when a user swipes on my collectionview. However the action only works when i swipe from my navigation bar, no where else. I would like it to hide on swipe from my collection view which of course takes up most of the view.
A mini video of the issue
override func viewDidLoad() {
navigationController?.hidesBarsOnSwipe = true
}

Is the view controller embedded in the navigation controller?

Swift 4, Swift 5:
Hide your navigation bar when you scroll upside and similarly show when you scroll downside.
scrollViewDidScroll() method will get called every time when you scroll your CollectionView, TableView, ScrollView etc.
Try this code:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Check the scroll direction here
if(scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0) {
print("Show")
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.setToolbarHidden(false, animated: true)
}
else {
print("Hide")
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.setToolbarHidden(true, animated: true)
}
}

Related

How to hide Navigation Bar, when my wkwebview start scrolling?

I am using WKWebview to show my content. When I scroll webview, I would like to hide my navbar. I use below code and nothing is happen.
navigationController?.hidesBarsOnSwipe = true
Can someone tell me, how to do this.
Add top constraint for the webView with superview, set hidesBarsOnSwipe to true, and then load the URL (check screenshot for reference):
One of the ways through code is you can use the scrollview delegate method to identify if webview is scrolled. And there you can use navigationController?.setNavigationBarHidden(isHidden, animated: true).
Something like this:
set scrollview delegate in viewdidload self.webView.scrollView.delegate = self. And then use
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView.superview).y > 0
{
print("up")
navigationController?.setNavigationBarHidden(false, animated: true)
}
else
{
print("down")
navigationController?.setNavigationBarHidden(true, animated: true)
}
}

Hide and show navigation bar for specific view without laggy animation

Is there a way to show navigation view on one view and not show it on another at the same time?
The problem: I have two view controllers - table and description view (called on cell click).
Table got a navigation bar, while description view - don't have it.
Table view:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
Description view controller:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
Everything works fine, but when i swipe for half screen back to table (keeping finger on screen, watching both views) - i don't see navigation bar (which works as expected with that code), and when i release finger - whole table view jumps, because nav bar is shown.
Is there a way to keep not seeing nav bar in description view and see it all the time in table view?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
You can hide the navigation bar while doing the segue (earlier). If you're doing it programmatically:
yourVCToBePushed.navigationController?.isNavigationBarHidden = true
If you're doing it in the storyboard, do similarly inside prepareForSegue:
let yourVCToBePushed = segue.destination as! YourVCToBePushed (type)
yourVCToBePushed.navigationController?.isNavigationBarHidden = true
You can also create your own "navigationView" inside tableView header, and add buttons there.

How to fix why status bar space behind ScrollView in Xcode?

I added a scroll view to my view which is hover the status bar (I hid it). The scroll view is working fine, but when I'm scrolling to the top, I have a white space which disappears when I tap on my screen, and appears again when I scroll down then top.
I noticed that the scroll bar is not going to the top of my view, but stopped at the status bar.
Here are screenshots which show you what I mean.
Here I'm at the top of my view but the scroll bar isn't:
Here is the same view with the white status bar which appears when I scroll top again:
It disappear when I tap on my screen or scroll down.
Here are my constraints:
I think it's a problem of Layout Margin or something like that, but I don't what I should change?
I hide the status bar like that in my view controller:
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
super.viewWillAppear(animated)
}
EDIT: Even if I comment the line which hides the status bar, I still have the same problem with my scroll view. So the problem doesn't come from how I hide it.
As Sam said, I changed the content insets to "Never" on the scroll view and it works.
While unrelated to your question, I have to react to the way you hide the status bar - the proper way is to override prefersStatusBarHidden in your view controller and call self.setNeedsStatusBarAppearanceUpdate() in your viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
return true
}
UPDATE
Since your view controller is inside of a UINavigationViewController, you need to override childViewControllerForStatusBarHidden in UINavigationViewController to use visibleViewController as the controller to determine status bar hidden (I added override to childViewControllerForStatusBarStyle for the consistence):
extension UINavigationController {
open override var childViewControllerForStatusBarStyle: UIViewController? {
return visibleViewController
}
open override var childViewControllerForStatusBarHidden: UIViewController? {
return visibleViewController
}
}

Hide Navigation Bar in Specific View - Swift 3

I have NavigationController that handles navigation through my app.
According to my design, the very first view should have no visible NavigationBar. All the others after, will.
In this FirstView, I'm using this so far to hide the NavBar, inside the ViewDidLoad:
self.navigationController?.isNavigationBarHidden = true
From this FirstView I can access other Views. In these other views I show the NavBar using:
self.navigationController?.isNavigationBarHidden = false
My problem is that:
When I navigate from a View with Visible NavBar, back to the FirstView with the Hidden NavBar, the NavBar is now visible.
Basically the NavBar only hides the very first time then shows if I use the back button.
How Can I Prevent this ?
Thank you!
Move that code to viewWillAppear() instead of viewDidLoad().
viewDidLoad() is only called once per instantiated view controller, whereas viewWillAppear() is called whenever the view controller is about to be presented on screen.
You can read more about the view controller lifecycle here.
Write below code in your FirstViewController's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = true
}
And in your SecondViewController's viewWillAppear method write below code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}
Do not try to hide and show nav bar in viewWillAppear and viewWillDisappear subsequetly in FirstViewController.
You can use this function to hide NavigationBar with cool animation:
func setupAnimationForNavigationBar(caseOfFunction: Bool) {
if caseOfFunction == true {
UIView.animate(withDuration: 0.5) {
self.navigationController?.navigationBar.transform = CGAffineTransform(translationX: 0, y: -200)
}
} else {
UIView.animate(withDuration: 0.5, animations: {
self.navigationController?.navigationBar.transform = CGAffineTransform.identity
})
}
}
If you want to hide NavigationBar, so set it "True" and if you want to call NavigationBar again, set it "False"

Tableview cut the last row but correct after back

I have one tableview that cut off in the last row of the cell. But when i click one cell, and click back from the next viewcontroller, my tableview is correctly shown all the cell.
Whast wrong with my tableview?
I set my tableview like this in home controller:
in ViewDidLoad:
self.automaticallyAdjustsScrollViewInsets = false
override func viewDidLayoutSubviews() {
self.myTableView.frame = self.view.bounds
}
and present the next view controller like this:
self.navigationController?.pushViewController(nextVC, animated: true)
and navigate back like this:
#IBAction func back(){
self.navigationController?.popViewControllerAnimated(true)
}
use autolayout. set top,bottom,leading and trailing constraints to it. and don't set frame of tableview like self.view.bounds
Try this code in viewWillAppear Method..
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated) // No need for semicolon
self.myTableView.frame = self.view.bounds
}

Resources