How to refresh Tab Bar Items in swift ios

那年仲夏 提交于 2021-02-07 13:12:44

问题


I do app like Instagram with tab bar items. In app I have simple user and company user.

I have main ViewController:

MainTabBarController: UITabBarController

with 5 tab bar items. And each item has own ViewController

I need refresh MainTabBarController when user is Simple user it is 5 items and when user is Company user it is 4 items. How to refresh or reload without close app?

One solution I already do with UserDefaults, but need close app.

Platform iOS > 9.0, Swift 3.0


回答1:


Use setViewControllers(_:animated:)

myTabBarController.setViewControllers(myViewControllers, animated: true)



回答2:


I got solution: I divided my MainTabBarController to 3 classes:

  1. AnonymUserTabBarController. Set 5 tab bar items.
  2. SimpleUserTabBarController. Set 5 tab bar items.
  3. CompanyTabBarController. Set 4 tab bar items.

And change user 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)
}



回答3:


You can post notification to refresh the tabs based on user type, first set the observer in MainTabBarController and once the notification is fired, check the user type and refresh the 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 for posting the notification whenever user type changes just call

 NotificationCenter.default.post(Notification(name: .refreshAllTabs))



回答4:


Usually notifications are really not swifty at all, Swift encourages all programmers to use protocols over Notifications... What about delegation or setting up didSet option for variable? You don't need even the notification at all. I suppose that TabBar is pushed right after the login, so you just make class variable and right in didSet setup the viewControllers:

///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 according to viewControllers the rest.



来源:https://stackoverflow.com/questions/44262038/how-to-refresh-tab-bar-items-in-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!