Swift: present view controller in same context as other view controllers? - ios

Ok, I have an issue that I cant understand trying to present a view controller (the same instance every time, just like other tab item VCs) from an overall tab bar controller VC. My tab bar controller VC has 3 view controllers that it is connected to via storyboard, so 3 tab bar items appear on the tab bar. When the selectedIndex is changed, these view controllers just appear right there below the subviews of the Tab Bar Controller VC.
These subviews that should always be on top are the nav bar at the top and tab bar at bottom:
And this is great for those 3 view controllers. Problem is I need to access 1 instance of ANOTHER view controller that is NOT shown in the tab bar buttons via a button in the nav bar here.
My problem is no matter how I present it, this VC always pops OVER the tab bar controller VC, covering the tab bar and nav bar.
here I make sure only 1 instance is made:
if podcastVC == nil {
//print("IT IS NIL")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
podcastVC = storyboard.instantiateViewController(withIdentifier: "podcast") as! PodcastViewController
//*NOTE: have to set other vars too, this is temp
podcastVC.urlStr = currentTrackUrl!
podcastVC.originalUrl = currentTrackUrl!
AudioPlayerManager.shared.play(urlString: podcastVC.urlStr)
}
self.show(podcastVC, sender: self)
podcastVC.modalPresentationStyle = .currentContext
podcastVC.definesPresentationContext = false
[1]: https://i.stack.imgur.com/1d6MZ.png
as shown by Swift: How to return to the same instance of my UIViewController
How can I make that VC present in the same context as the tab bar items? I have tried setting the layer of the nav bar to a z position much higher (like 10) but nothing works. What is wrong?

Modal view :
Can works for all view controllers
Is over all other view and need to be pop programatically (adding a button back manually for example)
Push View :
Only works in navigation controllers
Add automatically a back button in the navigationController
you should push VC and it will keep tabbar and nav
you can change modal present style

Related

Status bar won't hide if view controller is presented

I have multiple storyboards in my project. I have a home page view controller in one storyboard, and I have a Setup view controller embedded in a navigation controller in a separate storyboard. Now when I Present the setup view controller navigation controller from the homepage view controller, the status bar won't hide. But when I set the setup view controllers storyboard as the main storyboard file base in the info.plist and the setup view controller navigation controller is the first view presented then the status bar will hide. I'm using the code below to hide the status bar. Can someone show me how to hide the status bar when the status view controller is presented by another storyboard view controller instead of being set as the first view controller. Here is the code I'm using to hide the status bar,
override var prefersStatusBarHidden: Bool {
return true
}
You can hide status bar in a condition.. You need to add another Window Object over the status bar.
let stautsBarWindow = UIWindow(frame: UIScreen.main.bounds)
stautsBarWindow.backgroundColor = UIColor.clear
//Instead of Presenting just assign your viewController in below line it will hide your statusBar as well.
stautsBarWindow.rootViewController = yourSideMenuViewController
stautsBarWindow.windowLevel = UIWindowLevelStatusBar
stautsBarWindow.isHidden = false

Swift 3 - How do I prevent instantiated view controllers from having their background disappear behind the tab and navigation bars?

I am using Swift 3, Xcode 8.2.
I have a Storyboard with a Navigation controller and a connected Table View Controller. This table has cells that, when clicked, link to custom table view controllers that I instantiate each time.
let detailVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "dvc") as! ResultsVC
detailVC.view.backgroundColor = UIColor.white
detailVC.edgesForExtendedLayout = [];
But this newly created VC seems to have its background disappear behind the top nav bar and bottom tab bar because its colors change. How do I prevent this from happening?
This is expected behavior (I'm assuming that you're pushing the newly-instantiated VC with your navigation controller). If your views are hidden behind the navigation bar, do one of two things: 1) change the frame of your new VC's view, so that the y coord is equal to your navigation bar's height, or 2) push the views from IB, not in code, and this will be handled for you.

TableViewController (under a Navigation Controller) is invisible after I programatically switched to it in a UITabBarController

I have a (navigation controller -> table view controller) under my tab bar controller.
After switching to it with:
let rootViewController = (appDelegate.window?.rootViewController as? UITabBarController)!
// Switch tab to interests screen.
rootViewController.selectedIndex = 1
The screen takes a few seconds to update the change. Even though my tab bar controller shows that I have selected the tab.
What's wrong?

how to call tab bar view controller by click button function in swift

I have one common tab bar view controller.And i have connected 3 other view controllers with navigation bar and i connect this 3 view controllers with the main tab bar view controller.So now i have 3 tab bar items with 3 view controller.
that is 1.Home 2. Cart 3. feedback
This is fine !!
And now my first view controller(That is my first tab bar view controller) have some table view data and with detail view controller.And in my detail view controller i have one button called "Go to cart".
So the actual flows is when user press Go to cart.It have to move to cart` view controller.Now its moving.
But the problem is .I was not able to see my tab bar items and other items .But if i go normal to each tab bar items its showing all tab bar items.
When i go from my Detail view button click to my Cart tab bar view controller..Then no tab bar items are showing down.
I did all this like demo prototype.Like i drag from button to cart tab bar view controller navigation bar.
But its not showing tab bar why.Please help me out.I don't have any single solution to solve this.I am using swift 2.2.
Thanks.
Updated :
So i need to code some thing like this Right?.But i don't know how to handle this code:
let barViewControllers = sender.destinationViewController as! UITabBarController
let nav = barViewControllers.viewControllers![1] as! UINavigationController
let destinationViewController = nav.topViewController as! CartVC
But i am getting crash in first line :
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Please help me out
Here is storyboard similar to yours.
Now remove segue from button "go to cart" which is similar to "show second tab" button in my storyboard
Instead provide action to it as follows,
#IBAction func showSecondTab(sender: AnyObject) {
let navController = self.tabBarController?.viewControllers![1] as! UINavigationController
///secondviewcontroller in your case is cart
let secondViewController = navController.viewControllers[0] as! SecondViewController
//set values you want to pass
//lets say I want to pass name to secondVC
secondViewController.name = "ABCD"
self.tabBarController?.selectedIndex = 1
}
you can print and check values in viewDidLoad of secondVC(CartVC).

Edit button not displayed in UITabBarController's MoreNavigationController

A UITabBarController is being pushed onto the stack:
let presenter = presentingViewController as! UINavigationController
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
presenter.pushViewController(tabvc, animated: true)
Once presented the more tab button correctly shows, but the edit button to rearrange the tab bars does not. According to the docs on the MoreNavigationController:
The interface for the standard More item includes an Edit button that
allows the user to reconfigure the tab bar. By default, the user is
allowed to rearrange all items on the tab bar. If you do not want the
user to modify some items, though, you can remove the appropriate view
controllers from the array in the customizableViewControllers
property.
My guess is that the tab bar is not happy being in a navigation controller. Any ideas on bringing the edit button back?
You can have both a UINavigationController and a UITabBarController ; using Storyboard helps understand the issue better, any of these solutions will work:
Start out with a UITabBarController as initial view controller
Use presentViewController instead of pushViewController
Use a modal Storyboard segue to perform a modal presentation
Swap out the rootViewController dynamically
Initial View Controller Design
When the Tab Bar Controller is initial View Controller, the Edit button is displayed normally.
Pushed Design
Another Navigation Controller is initial View Controller, using one of 5 adaptive Action Segue:
Show
Custom
-> No Edit button, since it is in direct conflict with the parent UITableViewController.
Show Detail
Present Modally
Popover Presentation
-> Edit button displayed as expected.
Code
1. Program Modal
Using the exact code presented in the question, change the last line:
let presenter = presentingViewController as! UINavigationController
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
presenter.presentViewController(tabvc, animated: true, completion: nil)
2. Storyboard Modal
keeping with the Storyboard theme, create a segue of the correct type, assign an identifier (i.e. presentModallySegue) and the 5 lines above become this single line:
self.performSegueWithIdentifier("presentModallySegue", sender: self)
3. root Swap
A more drastic solution involves swapping out the root view controller at the window level:
let tabvc = UITabBarController()
tabvc.viewControllers = vcs
tabvc.customizableViewControllers = vcs
self.view.window!.rootViewController = tabvc
Conclusion
Either change your design to adopt the Tab Bar Controller as the initial View Controller, or present the Tab Bar Controller modally.
The reason is that navigation bar of your presenter overlaps with the navigation bar of More section.
If you don't show the navigation bar for you navigation controller, you will be able to see the Edit button again when you tap on the More tab.

Resources