I need write some code to switch the view to another tab when the iOS app starts (so, for example, the second tab is shown by default rather than the first).
I'm new to Swift, and have worked out the following:
The code should probably go in the override func viewDidLoad() function of the ViewController of the first tab.
The following code shows the second ViewController, but not with the tab bar at the bottom (vcOptions is the second ViewController tab item:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)
I think the answer may lie in using the UITabbarController.selectedIndex = 1, but not quite sure how to implement this.
If your window rootViewController is UITabbarController(which is in most cases) then you can access tabbar in didFinishLaunchingWithOptions in the AppDelegate file.
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
This will open the tab with the index given (1) in selectedIndex.
If you do this in viewDidLoad of your firstViewController, you need to manage by flag or another way to keep track of the selected tab. The best place to do this in didFinishLaunchingWithOptions of your AppDelegate file or rootViewController custom class viewDidLoad.
Swift 3
You can add this code to the default view controller (index 0) in your tabBarController:
override func viewWillAppear(_ animated: Bool) {
_ = self.tabBarController?.selectedIndex = 1
}
Upon load, this would automatically move the tab to the second item in the list, but also allow the user to manually go back to that view at any time.
1.Create a new class which supers UITabBarController.
E.g:
class xxx: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
2.Add the following code to the function viewDidLoad():
self.selectedIndex = 1; //set the tab index you want to show here, start from 0
3.Go to storyboard, and set the Custom Class of your Tab Bar Controller to this new class.
(MyVotes1 as the example in the pic)
The viewController has to be a child of UITabBarControllerDelegate. So you just need to add the following code on SWIFT 3
self.tabBarController?.selectedIndex = 1
To expand on #codester's answer, you don't need to check and then assign, you can do it in one step:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
Swift 5
//MARK:- if you are in UITabBarController
self.selectedIndex = 1
or
tabBarController?.selectedIndex = 1
if you are using UITabBarController
// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
if kAppDelegate.isLoggedIn == false {
switch tabBar.selectedItem?.title {
case "Favorite":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
case "Track Order":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
default:
break
}
}
}
In a typical application there is a UITabBarController and it embeds 3 or more UIViewController as its tabs. In such a case if you subclassed a UITabBarController as YourTabBarController then you can set the selected index simply by:
selectedIndex = 1 // Displays 2nd tab. The index starts from 0.
In case you are navigating to YourTabBarController from any other view, then in that view controller's prepare(for segue:) method you can do:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "SegueToYourTabBarController" {
if let destVC = segue.destination as? YourTabBarController {
destVC.selectedIndex = 0
}
}
I am using this way of setting tab with Xcode 10 and Swift 4.2.
Try
DispatchQueue.main.async {
self.tabBarController?.selectedIndex = 1
}
Just to update, following iOS 13, we now have SceneDelegates. So one might choose to put the desired tab selection in SceneDelegate.swift as follows:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
}
If you want to do this from code that you are writing as part of a particular view controller, like in response to a button press or something, you can do this:
#IBAction func pushSearchButton(_ sender: UIButton?) {
if let tabBarController = self.navigationController?.tabBarController {
tabBarController.selectedIndex = 1
}
}
And you can also add code to handle tab switching using the UITabBarControllerDelegate methods. Using tags on the base view controllers of each tab, you can see where you are and act accordingly: For example
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// if we didn't change tabs, don't do anything
if tabBarController.selectedViewController?.tabBarItem.tag == viewController.tabBarItem.tag {
return false
}
if viewController.tabBarItem.tag == 4096 { // some particular tab
// do stuff appropriate for a transition to this particular tab
}
else if viewController.tabBarItem.tag == 2048 { // some other tab
// do stuff appropriate for a transition to this other tab
}
}
Swift 5
If your class is a subclass of UITabBarController, you can just do:
selectedViewController = viewControllers![yourIndex]
From any where in-app you can switch between tabs
struct Constants{
static var tabbarVC : TabbarViewController!
}
During initialization of tab bar view controller store its reference to Constants.tabbarVC
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "TabbarViewController") as! TabbarViewController
Constants.tabbarVC = vc
Finally by using this reference you can switch tabs
Constants.tabbarVC.selectedIndex = 2
Related
Currently I have a view controller that when you tap on the tab at index 3 (Map Tab) it loads a view controller that contains a map (Map A). NOTE: it should always load Map A every time the Map Tab is pressed. There is a chiclet at index 0 on the main tab that when tapped allows you to switch from Map A to Map B and then takes you to that map (This switch is done manually so to keep the tab bar on screen using the tab bar's selectedIndex which means the viewWillAppear() doesnt seem to be called). NOTE: Map A and Map B share the same viewController, a bool is used to differentiate which one to load in viewWillAppear..The issue I'm having is after the chiclet is pressed to switch from Map A to Map B, once I hit the Map Tab again on the tab bar it automatically loads the current map I was just on (Map B), but as stated earlier, when pressed from tab bar, it should only load Map A.
This is what I was trying but it still won't show the proper map after the tab has been pressed:
class MainTabBarController: UITabBarController {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
switch viewController {
case is MapViewController:
let nc = NotificationCenter.default
nc.post(name: Notification.Name("changeMapStatus"), object: nil)
}
}
class MapViewController: BaseViewController {
var mapBSelected: Bool = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
mapBSelected ? setupMapB() : setupMapA()
}
#objc func changeMapStatus() {
guard let mainTabController = tabBarController as? MainTabBarController else { return }
mainTabController.refreshMapTab()
self.MapBSelected = false
}
}
func refreshMapTab() {
let index = PrimaryFeatureTab.map.displayOrder// enum to determine tab order
DispatchQueue.main.async {
self.viewControllers?.remove(at: index)
self.viewControllers?.insert(PrimaryFeatureTab.map.rootViewController, at: index)
}
}
}
You can do the following
Create a closure in your FirstViewController like this:
enum MapType {
case mapA
case mapB
}
class MainViewController: UIViewController {
var mapSelectionClosure: ((MapType) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func aTapped(_ sender: Any) {
mapSelectionClosure?(.mapA)
}
#IBAction func bTapped(_ sender: Any) {
mapSelectionClosure?(.mapB)
}
}
Then you can set this closure in your MainTabBarController like this
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
guard let mainViewController = viewControllers?.first(where: { $0 is MainViewController }) as? MainViewController else {
return
}
mainViewController.mapSelectionClosure = { mapType in
self.setMapType(mapType)
}
}
func setMapType(_ type: MapType) {
guard let mapViewController = viewControllers?.first(where: { $0 is MapViewController }) as? MapViewController else {
return
}
mapViewController.selectedMapType = type
}
}
In MapViewController...
class MapViewController: BaseViewController {
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.setupMapA()
}
}
Now, when you leave the Map Tab, it will reset itself to Map A.
No need for any code in a custom TabBarController.
I have an UITabBarController and sometimes in didSelectItem delegate I need to pause the event and present a popup. If user confirmed the event resumes and if not, event will be canceled. Here's my code:
class YC_TabBarController: UITabBarController {
var prevIndex: Int!
var exitAction: (()->Bool)?
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
self.prevIndex = self.selectedIndex
if self.prevIndex == 2 {
guard self.exitAction != nil else {return}
//pause
let isExitAccepted: Bool = self.exitAction!()
//if true -> resume
//if false -> prevent from switching tab
}
}
}
How can I do that? Please Help
You should confirm to UITabBarControllerDelegate in first view controller and return false if the desired view controller is selected in shouldSelect viewController. Then you should show your popup view. In popup view ok/confirm button you can change selected view controller of the self.tabBarController
class ViewController: UIViewController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is SecondViewController {
//show alert
return false
} else {
return true
}
}
func popUpOkAction(_ sender:UIButton) {
if let secVC = self.tabBarController?.viewControllers?.first(where: { $0 is SecondViewController }) {
self.tabBarController?.selectedViewController = secVC
}
}
}
If you want to perform this from multiple view controllers rather than
firstViewController you can confirm to UITabBarControllerDelegate in
YC_TabBarController itself.
I am updating my existing app to include a SplitView for iPads.
I have it working with a UITabBar, but am having an issue with my masterViewController as it is generating a "duplicate" navigation bar that is covering my existing navigation items on all masterViewControllers (tabs), including searchBar on the search tab.
The code I have is:
AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
let splitViewController = self.window!.rootViewController as! UISplitViewController
splitViewController.delegate = self
splitViewController.preferredPrimaryColumnWidthFraction = 0.33
splitViewController.minimumPrimaryColumnWidth = 375
splitViewController.preferredDisplayMode = .allVisible
return true
}
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController:UIViewController, onto primaryViewController:UIViewController) -> Bool {
return true
}
The reason for having this in the AppDelegate is I saw an example where placing it hear will allow me not to require the code in each of the different Master Views (each tab). Have yet to test this as still working on the first master view.
Master View
override func viewDidLoad()
{
self.extendedLayoutIncludesOpaqueBars = true
self.navigationItem.hidesBackButton = true
// 3D Touch
if traitCollection.forceTouchCapability == .available {
registerForPreviewing(with: self as UIViewControllerPreviewingDelegate, sourceView: view)
ThreeDTouch = true
}
self.addSwitchVewButtonToNavigationBar()
self.addCategoryButtonToNavigationBar()
}
func addSwitchVewButtonToNavigationBar() {
let switchButton = UIButton(type: UIButtonType.custom)
let editImage = UIImage(named: "CollectionButton")?.withRenderingMode(.alwaysTemplate)
switchButton.setImage(editImage, for: .normal)
switchButton.addTarget(self, action: #selector(SpeciesViewController.onSwitchView), for: UIControlEvents.touchUpInside)
let switchButtonFinal = UIBarButtonItem(customView:switchButton)
self.navigationItem.rightBarButtonItem = switchButtonFinal
}
#IBAction func onSwitchView(_ sender: UIBarButtonItem)
{
AppDelegate.getAppState().isListViewSelected = false
let speciesColletion = storyboard?.instantiateViewController(withIdentifier: Resource.SpeciesCollectionStoryboard) as! SpeciesCollectionViewController
self.navigationController?.viewControllers = [speciesColletion]
}
Originally, the onSwitchViewButton was embedded using the IB, but did not work. This is the same system used for the addFavorite on the Detail View.
The problem that you addressing wrong UINavigation controller. I assume before splitting both master and SpeciesViewController existed in same navigation environment, and use same navigation controller. But in split view they don't. Your detail controller is actually UINavigation controller you are looking for, that had to control all navigation, and had to have buttons you need. You can get it from master as:
guard let split = splitViewController, let navController = split.viewControllers.last as? UINavigationController else { return }
And make sure that you split controller not embedded into another UINavigationController (reason for second navigation bar).
EDIT:
Function to return detail's Nav Controller:
var detailsNavigationController: UINavigationController? {
return splitViewController?.viewControllers.last as? UINavigationController
}
To access blue call detailsNavController, to access red use navigationController.
I have a UITabBarController() that I use and assign in AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
showTabBar()
return true
}
func showTabBar() {
let tabBarVC = TabBarVC()
if let window = self.window {
window.rootViewController = tabBarVC
}
}
I have the following key is in info.plist:
In my Target under General, I have the following setting:
I use the following code in one of my tabs to hide the Status Bar:
class ViewController: UIViewController {
var statusBarShouldBeHidden = false
override func viewDidLoad() {
super.viewDidLoad()
}
override var prefersStatusBarHidden: Bool {
return statusBarShouldBeHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
#IBAction func buttonHideShowStatusBarTapped(_ sender: UIButton) {
statusBarShouldBeHidden = !statusBarShouldBeHidden
UIView.animate(withDuration: 0.25) {
self.setNeedsStatusBarAppearanceUpdate()
print("animating")
}
}
}
When the button is tapped, "animating" prints in the log; however, the status bar does not hide.
I am not sure if this is related to UITabBarController(), but the code above seems to work fine in a project without it.
How can I hide the status bar in iOS10 when using UITabBarController()?
You have taken TabBarVC as UIViewController subclass rather than UITabBarController subclass and then initialised and added the UITabBarController instance to it's view, I think TabBarVC should be subclass of UITabBarController and should be the rootViewController of the window. If you change the TabBarVC to subclass of UITabbarViewController status bar is working fine. Check the code below
class TabBarVC: UITabBarController, UITabBarControllerDelegate, UINavigationControllerDelegate {
//var mainTabBarController = UITabBarController() //not needed
init() {
super.init(nibName: nil, bundle: nil)
self.delegate = self
self.navigationController?.delegate = self
self.selectedIndex = 0
self.customizableViewControllers = []
self.setViewControllers(self.topLevelControllers(), animated: false)
}
You need to setNeedsStatusBarAppearanceUpdate() in your root view controller, i.e. TabBarVC. Here is the solution:
Override prefersStatusBarHidden in TabBarVC to return value of selectedViewController
override var prefersStatusBarHidden: Bool {
return mainTabBarController.selectedViewController?.prefersStatusBarHidden ?? false
}
Add reference to TabBarVC in ViewController class
var tabBarVC: UIViewController?
Set tabBarVC variable on topLevelControllers() method
let one = self.viewControllerFromStoryBoard(storyboardName: "One",
sceneName: "Initial",
iconName: "",
title: "Tab One") as! ViewController
one.tabBarVC = self
Finally, on your #IBAction update your status bar
self.tabBarVC?.setNeedsStatusBarAppearanceUpdate()
I need write some code to switch the view to another tab when the iOS app starts (so, for example, the second tab is shown by default rather than the first).
I'm new to Swift, and have worked out the following:
The code should probably go in the override func viewDidLoad() function of the ViewController of the first tab.
The following code shows the second ViewController, but not with the tab bar at the bottom (vcOptions is the second ViewController tab item:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)
I think the answer may lie in using the UITabbarController.selectedIndex = 1, but not quite sure how to implement this.
If your window rootViewController is UITabbarController(which is in most cases) then you can access tabbar in didFinishLaunchingWithOptions in the AppDelegate file.
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
This will open the tab with the index given (1) in selectedIndex.
If you do this in viewDidLoad of your firstViewController, you need to manage by flag or another way to keep track of the selected tab. The best place to do this in didFinishLaunchingWithOptions of your AppDelegate file or rootViewController custom class viewDidLoad.
Swift 3
You can add this code to the default view controller (index 0) in your tabBarController:
override func viewWillAppear(_ animated: Bool) {
_ = self.tabBarController?.selectedIndex = 1
}
Upon load, this would automatically move the tab to the second item in the list, but also allow the user to manually go back to that view at any time.
1.Create a new class which supers UITabBarController.
E.g:
class xxx: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
2.Add the following code to the function viewDidLoad():
self.selectedIndex = 1; //set the tab index you want to show here, start from 0
3.Go to storyboard, and set the Custom Class of your Tab Bar Controller to this new class.
(MyVotes1 as the example in the pic)
The viewController has to be a child of UITabBarControllerDelegate. So you just need to add the following code on SWIFT 3
self.tabBarController?.selectedIndex = 1
To expand on #codester's answer, you don't need to check and then assign, you can do it in one step:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
Swift 5
//MARK:- if you are in UITabBarController
self.selectedIndex = 1
or
tabBarController?.selectedIndex = 1
if you are using UITabBarController
// UITabBarDelegate
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
if kAppDelegate.isLoggedIn == false {
switch tabBar.selectedItem?.title {
case "Favorite":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
case "Track Order":
DispatchQueue.main.async {
self.selectedIndex = 0
DispatchQueue.main.async {
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { timer in
self.showAlert(Constants.GlobalConstants.APP_NAME, message: "You are not login.")
}
}
}
break
default:
break
}
}
}
In a typical application there is a UITabBarController and it embeds 3 or more UIViewController as its tabs. In such a case if you subclassed a UITabBarController as YourTabBarController then you can set the selected index simply by:
selectedIndex = 1 // Displays 2nd tab. The index starts from 0.
In case you are navigating to YourTabBarController from any other view, then in that view controller's prepare(for segue:) method you can do:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "SegueToYourTabBarController" {
if let destVC = segue.destination as? YourTabBarController {
destVC.selectedIndex = 0
}
}
I am using this way of setting tab with Xcode 10 and Swift 4.2.
Try
DispatchQueue.main.async {
self.tabBarController?.selectedIndex = 1
}
Just to update, following iOS 13, we now have SceneDelegates. So one might choose to put the desired tab selection in SceneDelegate.swift as follows:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
}
If you want to do this from code that you are writing as part of a particular view controller, like in response to a button press or something, you can do this:
#IBAction func pushSearchButton(_ sender: UIButton?) {
if let tabBarController = self.navigationController?.tabBarController {
tabBarController.selectedIndex = 1
}
}
And you can also add code to handle tab switching using the UITabBarControllerDelegate methods. Using tags on the base view controllers of each tab, you can see where you are and act accordingly: For example
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// if we didn't change tabs, don't do anything
if tabBarController.selectedViewController?.tabBarItem.tag == viewController.tabBarItem.tag {
return false
}
if viewController.tabBarItem.tag == 4096 { // some particular tab
// do stuff appropriate for a transition to this particular tab
}
else if viewController.tabBarItem.tag == 2048 { // some other tab
// do stuff appropriate for a transition to this other tab
}
}
Swift 5
If your class is a subclass of UITabBarController, you can just do:
selectedViewController = viewControllers![yourIndex]
From any where in-app you can switch between tabs
struct Constants{
static var tabbarVC : TabbarViewController!
}
During initialization of tab bar view controller store its reference to Constants.tabbarVC
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "TabbarViewController") as! TabbarViewController
Constants.tabbarVC = vc
Finally by using this reference you can switch tabs
Constants.tabbarVC.selectedIndex = 2