Reputation: 153
How can I present a View modally from a tab bar controller so that the view goes over the actual one?
I want to build a view with a camera. Something just like "WhatsApp" or "Instagram" where there is a button in the middle that the user can click and the camera view shows up.
Additionally, the user should move the tab he was before when the close button was clicked.
This is how my ViewController is connected to the TabBarController:
Upvotes: 9
Views: 11653
Reputation: 420
I've had to implement something similar in an app I'm currently building, it's relatively straightforward to do, you need to implement a delegate method of UITabBarController
in order to achieve this.
The delegate method you need to implement is:
tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
Returning false from this method will stop the tab controller from selecting your tab, you then just need to implement your own logic to present the UIViewController
programatically.
Here's an example:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// If your view controller is emedded in a UINavigationController you will need to check if it's a UINavigationController and check that the root view controller is your desired controller (or subclass the navigation controller)
if viewController is YourViewControllerClass {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "storyboardID") as? YourViewControllerClass {
controller.modalPresentationStyle = .fullScreen
self.present(controller, animated: true, completion: nil)
}
return false
}
// Tells the tab bar to select other view controller as normal
return true
}
I've not tested the above code as my implementation is slightly different and has more variables. The general principle is the same.
Let me know how you get on and I'll update the answer if necessary.
Upvotes: 17
Reputation: 31645
Assuming that you are conforming to UITabBarControllerDelegate, you could implement:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// here, you should edit "0" to be matched with your selected item
// for instance, if there is 5 items and the desired item is in the middle, the compared value should be "2"
if tabBarController.selectedIndex == 0 {
// simply, you will need to get the desired view controller and persent it:
let desiredStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desiredViewController = desiredStoryboard.instantiateViewController(withIdentifier: "storyboard id")
present(desiredViewController, animated: true, completion: nil)
}
}
Upvotes: 5
Reputation: 215
let modalVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifier")
modalVC.modalTransitionStyle = .crossDissolve
modalVC.modalPresentationStyle = .full or .overfullscreen // please check which of the options work
self.present(modalVC, animated: true, completion: {
})
Upvotes: -1