标签栏未出现在applicationDidBecomeActive上

时间:2018-07-23 09:33:31

标签: ios swift uitabbarcontroller

要求-用户从后台tabBar打开“应用程序”时,所选索引应为2

我尝试了什么-

//For getting current visible controller -
    public extension UIWindow {
        public var visibleViewController: UIViewController? {
            return UIWindow.getVisibleViewControllerFrom(self.rootViewController)
        }

        public static func getVisibleViewControllerFrom(_ vc: UIViewController?) -> UIViewController? {
            if let nc = vc as? UINavigationController {
                return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController)
            } else if let tc = vc as? UITabBarController {
                return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController)
            } else {
                if let pvc = vc?.presentedViewController {
                    return UIWindow.getVisibleViewControllerFrom(pvc)
                } else {
                    return vc
                }
            }
        }
    }
let appdelegate = UIApplication.shared.delegate as! AppDelegate

    //Selected 2 tabbar -
         func applicationDidBecomeActive(_ application: UIApplication) {
                if appdelegate.window?.visibleViewController != nil {
                    appdelegate.window?.visibleViewController?.hidesBottomBarWhenPushed = false
                    appdelegate.window?.visibleViewController?.navigationController?.popToRootViewController(animated: true)

                    appdelegate.window?.visibleViewController?.tabBarController?.selectedIndex = 2
                    appdelegate.window?.visibleViewController?.tabBarController?.tabBar.isHidden = false
                    appdelegate.window?.bringSubview(toFront: (appdelegate.window?.visibleViewController?.tabBarController?.view)!)

                }
            }

代码工作正常,但问题是Tabbbar被隐藏了。为什么?

2 个答案:

答案 0 :(得分:0)

如果每个选项卡都有一个NavigationController,这将起作用

public getManagerConfig() {
  return this.http.get<any[]>('/api/v1/configuration/manager');
}

答案 1 :(得分:0)

  1. 您需要继承UITabBarViewController
  2. 覆盖viewWillAppear方法。
  3. 订阅UIApplicationWillEnterForeground通知。
  4. 在收到通知时设置selectedIndex。

    class HomeViewController: UITabBarController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector:#selector(applicationWillEnterForeground(notification:)), name: .UIApplicationWillEnterForeground, object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.removeObserver(self, name: .UIApplicationWillEnterForeground, object: nil)
    }
    
    @objc private func applicationWillEnterForeground(notification: Notification) {
        selectedIndex = 2
    }}