How to show and hide navigationBar? - swift

I have two UITableViewController, and in each of them NavigationBar is visible; then I have a simple UIViewController. So I'd like to make NavigationBar invisible only in the third view. I tried
self.navigationController?.navigationBarHidden = true
but this make navigationBar invisibile in every view, after I leave the third one.
I also tried
override func prefersStatusBarHidden() -> Bool {
return true
}
This is my application scheme: only in "DettaglioController" I'd like to make navigationBar invisible.
Any ideas to solve?

Its just one line of code....
navigationController?.setNavigationBarHidden(true, animated: true)
In the ViewControllers viewWillAppear you can hide the NavigationBar like this, and in its viewWillDisappear you can show it back again

Related

How can I always show the UIScrollBar indicator?

When a UIViewController with a UIScrollView is visible on screen, I want to show the UIScrollView scrollbar indicator.
Is there a way to show the indicator?
You can show the scrollbar momentarily with .flashScrollIndicators when the view controller has been added to the view hierarchy and is potentially visible to the user.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.flashScrollIndicators()
}

Navigation item views disappearing when using multiple search bars and interactive pop gesture

I'm using multiple UISearchBar's as titleViews in UINavigationControllers embedded in a UITabBarController. This all works just fine when I push and pop view controllers using the back button. But, the search bar starts to disappear when I pop using the interactive pop gesture.
I'm not sure how to fix this.
I've tried setting the titleView of the navigationItem in viewWillAppear and viewDidAppear but both didn't work.
The search bar accepts input and touches, it's just not visible.
Here's the viewWillAppear code:
override func viewWillAppear(_ animated: Bool) {
print("view will appear from base search")
super.viewWillAppear(animated)
print(self.searchController.searchBar)
self.definesPresentationContext = true
self.navigationItem.titleView = nil
self.navigationItem.titleView = self.searchController.searchBar
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
Here are some photos of what happens:
https://imgur.com/a/OQ7kb1q
The first photo is the homepage.
The second photo is a user typing in the search query.
The third photo is when a user returns after executing interactive pop gesture.
The fourth photo shows that you can still type in the search bar even though it's not visible.
The fifth photo shows you can still hit the cancel button next to the search bar even though that's not visible.
I fixed this by setting: self.navigationItem.searchController = searchController
instead of setting:
self.navigationItem.titleView = searchController.searchBar

bottom tabbar disappears and leave black when pushed from UITableViewController to UIViewController

In my use case, I want to hide the bottom tabbar when navigating away from UITabbarController.
I was using
let vc = storyboard?.instantiateViewController(withIdentifier: tableData[indexPath.row]["vcIdentifier"]!)
self.hidesBottomBarWhenPushed = true
self.show(vc!, sender: self)
It sorta works, because the pushed view controller doesn't have tabbar at bottom. However, as soon as I click on navigate, the bottom tabbar of the "sender" view controller vanishes and leaves black area.
Please let me know if you need to have more information about anything. Thanks a lot in advance!
If the pushed view controller doesn't have a tab bar at the bottom, you can add this lifecycle of view controller codes.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.tabBar.isHidden = true
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
tabBarController?.tabBar.isHidden = false
}
you can use segue if you want to hide tabbar while going to the next screen. it will automatically hide it.

navigationController?.navigationBar.isUserInteractionEnabled not working as expected

For the following viewController hierarchy, isUserInteractionEnabled doesn't appear to be working as expected.
NavigationController(ViewController A) --- pushes to ---> NavigationController(ViewController B)
In ViewController A's viewDidAppear method I set navigationController?.navigationBar.isUserInteractionEnabled to false and set it to true in ViewController B's viewDidAppear method. However, upon popping ViewController B and returning to ViewController A, the navigation bar remains enabled for user interaction. Any thoughts as why this may be happening are greatly appreciated, thanks in advance!
That seems to be a bug for which you could get around by doing that on the main thread:
override func viewDidAppear(_ animated: Bool) {
//...
DispatchQueue.main.async {
self.navigationController?.navigationBar.isUserInteractionEnabled = false
}
}
But this still leaves a millisecond window where the navigationBar's interaction is enabled.
You have to be really quick.
However...
I wouldn't recommend what you're doing; i.e. disabling the navigationBar.
You could lose the back ability, if it had one, because you're just disabling the navigationBar entirely.
Suggestion:
Since every viewController in the navigation stack has it's own navigationItem, that contains it's own set of barButtonItems, I would recommend you keep references of the UIBarButtonItem and enable/disable them explicitly.
i.e.
#IBOutlet var myBarButtonItem: UIBarButtonItem!
override func viewDidAppear(_ animated: Bool) {
//...
myBarButtonItem.isEnabled = false
}
Furthermore, the state of this barButtonItem is handled in this viewController itself and you need not do things like self.navigationController?.navigationBar.isUserInteractionEnabled = true elsewhere.

TextView in iOS 8, Navigation bar is over Text View

Hello I am using iOS 8 (swift)
Now I have this
https://www.dropbox.com/s/tu9issn0b61aphh/IMG_1304.PNG?dl=0
But I need this, so that the textview is showing the beginning of the text!
https://www.dropbox.com/s/0ggzndfr90vcrnj/IMG_1305.PNG?dl=0
I have try lots off stuff, but without success!
I need your help?
In your UIViewController implement the following function
override func viewDidLayoutSubviews() {
myTextView.setContentOffset(CGPointZero, animated: false)
}
Also in viewDidLoad set the following property
automaticallyAdjustsScrollViewInsets = false
I'm unsure as to why this happens, but this will prevent it from happening.
In the attributes inspector of your root view controller be sure that "Under Top Bars" is un-checked.