Switching to a TabBar tab view programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Switching to a specific tab programmatically is done differently depending on the framework. In UIKit, set selectedIndex or selectedViewController on UITabBarController. In SwiftUI, bind a @State variable to TabView(selection:) and change that variable. In Android with Material Tabs, call TabLayout.getTabAt(index).select(). This is needed when deep links, push notifications, or user actions in one tab should navigate to another tab.
UIKit (Swift)
Using selectedIndex
Using selectedViewController
From AppDelegate (Deep Links)
Handling UITabBarControllerDelegate
Control whether a tab switch should be allowed:
SwiftUI
TabView with Selection Binding
Switching from a Child View
Pass the binding or use a closure:
Using String Tags
Android (Kotlin)
TabLayout with ViewPager2
Bottom Navigation (Jetpack)
Jetpack Compose
React Native
Common Pitfalls
- Setting
selectedIndexbefore tabs are loaded: In UIKit, settingselectedIndexinviewDidLoadof the tab bar controller before all child view controllers are added has no effect. Set it inviewDidAppearor after adding all tabs. - Forgetting
.tag()in SwiftUI TabView: Without.tag()on each tab, theselectionbinding cannot match tabs. Tags must be the same type as the@Statevariable (e.g., allIntor allString). - Not syncing TabLayout with ViewPager2 on Android: Calling
tabLayout.getTabAt(index)?.select()alone does not scroll the ViewPager. Also setviewPager.currentItem = indexto keep both in sync. - Animating tab switches when not desired: UIKit animates the tab switch by default with
selectedViewController. To switch without animation, useselectedIndexdirectly. In SwiftUI, wrap the assignment inwithAnimation(nil)to suppress animation. - Switching tabs from a background thread: UI updates must happen on the main thread. In UIKit, wrap in
DispatchQueue.main.async { }. In SwiftUI, ensure@Statechanges happen on the@MainActor.
Summary
- In UIKit, set
tabBarController.selectedIndexorselectedViewControllerto switch tabs - In SwiftUI, bind a
@Statevariable toTabView(selection:)and update the variable - In Android, use
tabLayout.getTabAt(index)?.select()withviewPager.currentItem = index - In React Native, call
navigation.navigate('TabName')from any screen - Always ensure tab tags/indices are set correctly and UI updates happen on the main thread
Related reading
- Switching to landscape mode in Android Emulator
- Symbol not found kUTTypeImage
- Sync data between Android App and webserver
- Sync in Android sqlite and sql server crud operation in two ways
- Tab not taking full width on Tablet device Using android.support.design.widget.TabLayout
- TabLayout tab selection
- Table Header Views in StoryBoards
- Tableview scroll content when keyboard shows
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.