Member-only story
SwiftUI: Ways to Show Badge on Tab Bar Item
2 min readFeb 15, 2021
Xcode 12.4 — iOS 14
Using wrapped TabViewController within TabView
SwiftUI’s TabView is not native SwiftUI component but rather a wrapper on UIKit’s UITabBarController. If can get access to UITabBarController, We can simply use UIKit APIs to set badge.
- To get UITabBarController, we can iterate from UIApplication window to rootViewController and subsequent child until we get instance of UITabBarController.
- Every time View State change SwiftUI view updates, It also refreshes tab view resulting resetting UIKit’s Badge. So we will also need to set a KVO on TabViewItem so that we can override this reset.
Demo: