How to update tab bar items in swift ios
I am making an app like Instagram
with tab bar items. In the application I have simple user
and company user
.
I have a main ViewController:
MainTabBarController: UITabBarController
with 5 panel points. And each element has its ownViewController
I need to update MainTabBarController
when user simple user
is 5 items and when user company user
is 4 items. How do I update or reload the app without closing?
One of the solutions I already do with UserDefaults, but you need to close the application.
IOS platform> 9.0, Swift 3.0
source to share
I got the solution: I split my MainTabBarController into 3 classes:
- AnonymUserTabBarController. Set 5 tab bar items.
- SimpleUserTabBarController. Set 5 tab bar items.
- CompanyTabBarController. Set 4 positions on the tab.
And change the UI with animation:
func selectCompanyUser() {
guard let window = UIApplication.shared.keyWindow else {
return
}
guard let rootViewController = window.rootViewController else {
return
}
let viewController = CompanyTabBarController()
viewController.view.frame = rootViewController.view.frame
viewController.view.layoutIfNeeded()
UIView.transition(with: window, duration: 0.6, options: .transitionFlipFromLeft, animations: {
window.rootViewController = viewController
}, completion: nil)
}
source to share
Use setViewControllers (_: animated :)
myTabBarController.setViewControllers(myViewControllers, animated: true)
source to share
Usually notifications are not really very frugal, Swift encourages all programmers to use protocols via notifications ... How about delegating or setting the didSet option on a variable? You don't even need a notification. I suppose the TabBar is pushed right after login, so you just create a class variable and set up the viewControllers right in the didSet:
///Considering UserType is enum with cases:
var currentUserType: UserType{
didSet{
currentUserType = .company ? self.viewControllers = /*array with 5 count*/ : self.viewControllers = /*array with 4 counts*/
}
}
and now just handle the rest according to the viewControllers.
source to share
You can send notifications to refresh tabs based on user type, first set observer in MainTabBarController and after notification is fired check user type and refresh tabs
extension Notification.Name {
static let refreshAllTabs = Notification.Name("RefreshAllTabs")
}
class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: .refreshAllTabs, object: nil, queue: nil) { (notification) in
//check if its a normal user or comapny user
if AppUser.shared.type == .normal {
self.viewControllers = [VC1, VC2, VC3, VC4, VC5]
} else {
self.viewControllers = [VC1, VC2, VC3, VC4]
}
}
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
now to send a notification every time the user type changes, just call
NotificationCenter.default.post(Notification(name: .refreshAllTabs))
source to share