Dismiss View Controller inside UITabController - ios

I've a project which contains a tab controller, as the main 'page'.
I want to add a UITabBar button item, which presents a view controller modaly, and within that view controller, add a dismiss button that dismisses that view controller and returns to the previous tab selection.
To clarify, it's something that the Medium app for iOS uses, when you click on the create post item, it presents it modaly and dismisses it when you want to.
I can present the view controller, but I can't dismiss it.
Hope I made myself understandable.
Example:

So I've managed to solve this as I wanted to, hope the answer will help someone in the future.
First of all, I set on my first loaded view controller, lets say my 'Home' view controller the tabBarController delegate to be my AppDelegate.swift file (this is personal preference), as:
self.tabBarController?.delegate = UIApplication.shared.delegate as? UITabBarControllerDelegate
Then in my AppDelegate.swift file i added to the class properties the delegate UITabBarControllerDelegate so I could access the tabBarController delegate functions, such as
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {}
Then edited the function to interact with my specific view controller.
The final version of the function:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is ViewControllerToPresentModaly {
if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "modalVC") {
tabBarController.present(newVC, animated: true)
return false
}
}
return true
}
Here if you want the regular tab behaviour, return true or your own return false
;)

Related

Show a ViewController that is not in UITabBarController's viewControllers list

I have a UITabBarController and i'm setting some ViewControllers to its viewControllers list like below
let tabBar = UITabBarController();
tabBar.viewControllers = vcs;
When view is loaded, the firs ViewController in vcs list will be shown.
What i want is, showing a UIViewController that is not in vcs list (and none of tabBar must be selected).
I want to show this UIViewController only when view is loaded for the first time. After that the user tapped one of the tabBarItems, i want to load the associated ViewController.
So my problem is 'Showing a UIViewController' that is not in vcs list
Look at UITabBarControllerDelegate method tabBarController(_:shouldSelect:).
Make yourself delegate of your UITabBarController, depending if you use Storyboard or not, it might be easier to create a custom class that inherit from UITabBarController be itself delegate and set that class into Storyboard.
var hasNotShownYet = false
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
guard hasNotShownYet else { return true /* Allow selection as "normal" */ }
presentCustomVCModally()
hasNotShowYet = true
return false
}

iOS Black screen when close modally presented view controller

I have a tabbar controller and suppose if the user goes to 3rd tab and in 3rd view controller, User presses a button and there I present the view controller as modal .
Now if user switched the tab let say tab 1 or 2 without closing the modally presented view controller, and mean while he comes back to tab 3, the user will still able to see the modally presented view controller, now if user closes the modally presented view controller here, then there is a black screen instead of view controller that opened the modally presented view controller.
The possible work around is that I hide the tabbar controller so that user must close modally presented the view controller first. but it is not the requirement right now. Due to some reasons I can not hide bottom tab bar controller.
Please suggest me how to do following
First of all when user switches tab after opening modally presented view controller, so when he closes the modally presented vc he must see the screen at the back
If point no 1 is not possible so when user goes back again to tab 3 (in which he started modally presented VC) there should not be modally presented view controller. In other words when user switched the tab and the modally presented view controller is opened, it must close, before moving to other tab.
You can do it with UITabBarControllerDelegate.
Option 1: Creating subclass of UITabBarController
class MyTabBar: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let currentlySelectedViewController = self.viewControllers?[self.selectedIndex] as? YourThirdTabViewController,
let presentedViewController = currentlySelectedViewController.presentedViewController {
presentedViewController.dismiss(animated: false, completion: nil)
}
return true
}
}
Here I am assuming that I have my own sub class of UITabBarController which is set as UITabBarControllerDelegate using self.delegate = self in view didLoad.
Option 2: Make TabBarController's ViewController UITabBarControllerDelegate in their ViewDidLoad
If you dont wanna have your own subclass of UITabBarController all you have to do is simply set your self as UITabBarControllerDelegate in each ViewController's ViewDidLoad
You can have a common protocol or extension to UIViewController and confirm UITabBarControllerDelegate to avoid code duplication if you decide to go down the path of not subclassing UITabBarController
protocol TabBarControllerProtocol: UITabBarControllerDelegate where Self: UIViewController {}
extension TabBarControllerProtocol {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let currentlySelectedViewController = self.tabBarController?.viewControllers?[self.tabBarController?.selectedIndex ?? 0] as? SomeViewController,
let presentedViewController = currentlySelectedViewController.presentedViewController {
presentedViewController.dismiss(animated: false, completion: nil)
}
return true
}
}
In whichever ViewController, you want this logic to kick in you can say
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
}
extension SomeViewController: TabBarControllerProtocol {}
Thats it.
shouldSelect viewController gets called every time user taps on tab to switch.
The if condition checks if ViewController at selectedIndex (already selected not the new one) is viewController of specific interest or not and also checks if this view controller has presented anything or not, if it has presented it will call dismiss on it
This way when user switches tab from 3rd tab to any other tab, if third tab VC has presented another view controller modally it will dismiss it before it switches tab.

Pass Values from TabBarController to child view controllers when selected

I currently have a Tab Bar Controller and two view controller connected to it. However I need to pass values from the TabBarController to the view controller that is selected. So like when a user selects the specific tab, I want to pass a certain value to that view controller. Right now, I have the view controller embedded from the storyboard. Thanks!
Implement didSelect
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let vc = viewController as? SecondVC {
vc.somePro = <#value#>
}
}

UITabBarController's viewControllers present modal controller issue

I have a UITabBarController with 4 viewControllers setup.
One of the controller has a button that present another controller (wrapped on UINavigationController) with the following setup:
self.definesPresentationContext = true
navController.modalPresentationStyle = .overCurrentContext
navController.modalTransitionStyle = .crossDissolve
self.present(navController, animated: true)
Until this point is working fine.
Now if I switch to another tab (while the previous modal is open), and return again to the tab that presented the modal (The screen is still there, that's ok). Then if i close the modal (from a Button), the modal is dismissed but the controller view's has gone (white), then if I switch to another tab and return to the tab again, the view load correctly.
Note: For this case, I need overCurrentContext, don't want to block UITabBarController (with fullScreen).. Also try with .currentContext, custom
If this is the same bug that I demonstrate here, the workaround I give is to prevent the user from switching to another tab while this tab is showing the presented view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
extension FirstViewController : UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return self.presentedViewController == nil
}
}

IOS - When tapped on a tab item, I need to check condition to show other view controller

In my app, I have UITabBarController, when user tapped on a tabbar item (e.g: tab index is 3), I want to check one condition (if...) to show different ViewController.
So my question is where to implement this condition function?
you need to set the delegate of the UITabBarController as below:
func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
if viewController is TabBarDelegate {
let v = viewController as! TabBarDelegate
v.didSelectTab(self)
}
}
try with create tabbar by button and view is a scrollview

Resources