2017-05-28 99 views
0

時,我有一個UITabBarController(),我使用,並指定在AppDelegateiOS10:隱藏狀態欄使用的UITabBarController()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    showTabBar() 
    return true 
} 

func showTabBar() { 
    let tabBarVC = TabBarVC() 
    if let window = self.window { 
     window.rootViewController = tabBarVC 
    } 
} 

我有以下關鍵是info.plist

enter image description here

在我的TargetGeneral,我有以下設置:

enter image description here

我用下面的代碼在我的標籤隱藏狀態欄的一個:

class ViewController: UIViewController { 

    var statusBarShouldBeHidden = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override var prefersStatusBarHidden: Bool { 
     return statusBarShouldBeHidden 
    } 

    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { 
     return .slide 
    } 


    @IBAction func buttonHideShowStatusBarTapped(_ sender: UIButton) { 
     statusBarShouldBeHidden = !statusBarShouldBeHidden 

     UIView.animate(withDuration: 0.25) { 
      self.setNeedsStatusBarAppearanceUpdate() 
      print("animating") 
     } 
    } 
} 

當按鈕被竊聽,「動畫」在日誌中打印;但是,狀態欄不會隱藏。

我不確定這是否與UITabBarController()有關,但上面的代碼似乎在沒有它的項目中正常工作。

如何在使用UITabBarController()時隱藏iOS10中的狀態欄?

回答

0

您已經採取TabBarVC爲UIViewController子類,而不是UITabBarController子類,然後初始化並添加了UITabBarController實例,它的觀點,我覺得TabBarVC應該是子類UITabBarController並且應該是窗口的rootViewController。如果您將TabBarVC更改爲UITabbarViewController狀態欄的子類正常工作。檢查下面的代碼

class TabBarVC: UITabBarController, UITabBarControllerDelegate, UINavigationControllerDelegate { 

    //var mainTabBarController = UITabBarController() //not needed 

    init() { 
     super.init(nibName: nil, bundle: nil) 
     self.delegate = self 
     self.navigationController?.delegate = self 
     self.selectedIndex = 0 
     self.customizableViewControllers = [] 
     self.setViewControllers(self.topLevelControllers(), animated: false) 
    } 
0

您需要在您的根視圖控制器中使用setNeedsStatusBarAppearanceUpdate(),即TabBarVC。這裏是解決方案:

覆蓋prefersStatusBarHiddenTabBarVC返回的selectedViewController

override var prefersStatusBarHidden: Bool { 
    return mainTabBarController.selectedViewController?.prefersStatusBarHidden ?? false 
} 

添加參考TabBarVC值在ViewController

var tabBarVC: UIViewController? 

設置tabBarVC變量上topLevelControllers()方法

let one = self.viewControllerFromStoryBoard(storyboardName: "One", 
                sceneName: "Initial", 
                iconName: "", 
                title: "Tab One") as! ViewController 
one.tabBarVC = self 

最後,在你的@IBAction更新您的狀態欄

self.tabBarVC?.setNeedsStatusBarAppearanceUpdate() 
-1

隱藏狀態欄。

在你的info.plist您可以添加狀態欄最初是隱藏YES