Swift, Navigation bar should not be shown on the login screen - swift

I have initialized Navigation controller from the root controller (viewController), and wrote down the code to hide it initially
self.navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
But when i go to next screen and click on the back button , i can see my navigation bar on my Login screen.
Ideally we can't show the navigationbar on login screen, What should i do to avoid this?

Login Screen ViewDidload write following line
navigationController?.setNavigationBarHidden(true, animated: true)
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: true)
}

Related

Presenting modal over modal add a gap between navigation bar and view content

Seems like New iOS 13 modal presentation has issue when presenting a modal over a modal. A gap will be appear between navigation bar and content. check the screenshot.
Refreshing UI fix the issue:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 13.0, *) {
navigationController?.navigationBar.setNeedsLayout()
}
}

small navigation title is showing for few seconds

I have set large title in viewWillAppear()
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Reports"
But still when I redirect to next VC and come back I can see navigation Title in small size for a while and then I see large title, anybody know why and how to fix it?
If you want the next VC to have largeTitleDisplayMode false, you could try to set it in viewWillDisappear() of the current VC, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = false
}
You should set this in the viewDidLoad of your view controller, not the viewWillAppear. It's the first part in the view lifecycle and where this work should be done

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.

Present below current view and not above

I'm trying to present a view controller below another presented view controller (like WhatsApp when you open camera and press gallery).
I tried many things but none worked ..
Use child view controller and set view of that added child view controller at the top of hierarchy. It will be top most element, so actual background of this view will be obscured, but that's the way to go.
//code inside UIViewController class
func addViewControllerAtBottom() {
let newVC = NewVCType() //just instantiate it
addChildViewController(newVC)
view.insertSubview(newVC.view, at: 0) //at 0 means it's first rendered, all others will be on top of it
}
You can reproduce this behavior by doing the following :
First, create a NavigationController with a root ViewController :
let navController = UINavigationController(rootViewController: firstController)
Then, present this navigationController with animated: false and in the completion of the present method, push your second ViewController, still with animated: false (to avoid weird animations) :
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
Here you go, you got a new navigation with 2 UIViewController, like WhatsApp.
Full code, wrapped into a button's action :
#IBAction func buttonTapped(_ sender: Any) {
let navController = UINavigationController(rootViewController: firstController)
present(navController, animated: false) {
navController.pushViewController(secondController, animated: false)
}
}

Nav BarItem Returning To Previous View

In the below figure, I have a "Back" button in the nav bar which I would want for it to close the Barcode Scanner tab and bring me to to the view I was prior to hitting the "Back button. How would that be possible?
Your request is improper UI. Tabs should not contain back button for going to previous tab, and I really hope that 'Close' the tab doesn't mean you want to remove it.
Other than that you can change active tab UITabBar.setSelectedItem. But really in your case don't do it.
View controllers that you load are all stacked in order of you viewing them. To go back to your previous view simply dismiss the last view controller. You can use this code:
#IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
Hopefully this should do the trick.
you can override back button in ViewDidLoad like :
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(self.back(_:)))
}
func back(_ sender: AnyObject) {
//if you want to go to previous view use this code
self.navigationController?.popViewController(animated: true)
//if you want to go to a tab bar view use this code
//index of your tab bar
tabBarController?.selectedIndex = 1
}