Tab bar controller push segue instead (Swift) - ios

I don't really know how to explain this but i have a tab bar controller and a qr code scanner controller, how can i make it perform a segue to the qr code scanner (exactly like the Instagram camera, when u tap on the camera icon, the whole tab bar disappears and segue to the camera page) instead of just tapping it. Thank you in advance for reading this.
Image of Storyboard

Don't link QR scanner controller into tabbar.
Link some empty VC to 3-d tab
Setup delegate for your tabbar controller
implement method:
public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.viewControllers!.last == viewController {
// present your vc
return false
}
return true
}

Related

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.

How to force a tab bar item to go to the root level of that item whenever its tapped

I'm new to iOS and Swift. In my storyboard I've set up a tab bar controller where the right most item is a "More" item that leads to a table view controller embedded in a navigation controller. Each item in the table view (static) goes to a view controller (say "View 1", "View 2", and "View 3").
The default behavior is as follows. Let's say I tap "More". Then I'm looking at the table with cells for "View 1", "View 2", and "View 3". Then let's say I tap "View 1". If I then click a different item in the tab bar controller, and then click "More" again, rather than take me to the table view, it will take me to "View 1" since that's the last thing I tapped when I was last in "More". I'd like the behavior to be that if I tap outside of "More" with another tab bar element, any time I tap "More" again, I want it to always take me to the table view, regardless of what I was viewing previously within "More".
Essentially, I want "More" to forget its state or reset its state.
How do I force this to happen?
I will walk you through an example to make sure that it is clear.
Consider that this is the storyboard:
It has a tabbar view controller connected to:
A view controller (the one on the top).
A table view controller which is embeded in navigation controller (the one on the bottom). It has tableview with a static cell, when tap on it, it pushes to another view controller.
So far so good! now, the first view controller class should conforms to UITabBarControllerDelegate and implements tabBarController(_:didSelect:) method, as follows:
class ViewController: UIViewController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tabBarController?.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// im my example the desired view controller is the second one
// it might be different in your case...
let secondVC = tabBarController.viewControllers?[1] as! UINavigationController
secondVC.popToRootViewController(animated: false)
}
}
Remark: if you have more that tow view controllers connected to the tabbar view controller, you should apply this code to the first view controller that should appear in the tabbar view controller.
And that's it!
Output:
Cheers up! hope this helped.
The code supplied works very well with one exception. If you have multiple tabbar items linked to one view controller then the indexed code tabBarController.viewControllers?[1] will only work on "1". It is possible to have "2" and "3" etc. By adding tabBarController.selectedIndex as the index it will always return back to root for that tabbar item. Below is how I fixed the code. I hope this helps someone:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// im my example the desired view controller is the second one
// it might be different in your case...
let secondVC = tabBarController.viewControllers?[tabBarController.selectedIndex] as! UINavigationController
secondVC.popToRootViewController(animated: false)
}
after making the class confirm to the tabBarController delegate just add this function
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.navigationController?.popToRootViewController(animated: false)
}

Dismiss View Controller inside UITabController

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
;)

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

Can a Tab Bar contain a "play" button

In my Tab Bar, I have a "My favorites" and "About Us" icon. These go to different View Controllers. I have made a UIButton that plays an audio file. How do I add that button to the Tab Bar at the bottom?
So when the button is tapped, it plays on the current scene, and does not go to a new scene.
You can prevent a tab from opening it's view controller by subclassing UITabBarController and implementing tabBarController(_:shouldSelectViewController:). From there you could perform your play action.
More on UITabBarControllerDelegate
In Swift your subclass would look like the following
class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let tabIndex = tabBarController.viewControllers?.indexOf(viewController)
if tabIndex == 2 {
// Perform your play action here
return false
}
return true
}
}
You can write a your tabBarController which has several buttons. each button has a tag. so, you can judge the button which you can't go to a new controller.

Resources