我遵循这个线程重写-preferredStatusBarStyle,但它没有被调用。 有什么选项我可以改变来启用它吗?(我在我的项目中使用xib。)


当前回答

我的应用程序使用了所有三个:UINavigationController, UISplitViewController, UITabBarController,因此这些似乎都接管了对状态栏的控制,并将导致preferedStatusBarStyle不为他们的孩子被调用。要覆盖此行为,您可以创建一个扩展,就像前面提到的其他答案一样。这是一个扩展,这三个,在Swift 4。希望苹果对这类事情能更清楚些。

extension UINavigationController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
}

extension UITabBarController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.childViewControllers.first
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.childViewControllers.first
    }
}

extension UISplitViewController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.childViewControllers.first
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.childViewControllers.first
    }
}

编辑:针对Swift 4.2 API更改的更新

extension UINavigationController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
}

extension UITabBarController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.children.first
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.children.first
    }
}

extension UISplitViewController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.children.first
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.children.first
    }
}

其他回答

如果有人在使用UISearchController时遇到这个问题。 只需要创建一个UISearchController的新子类,然后将下面的代码添加到这个类中:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

在UINavigationController上,preferredStatusBarStyle不会被调用,因为它的topViewController优先于self。所以,要在UINavigationController上调用preferredStatusBarStyle,你需要改变它的childForStatusBarStyle (Swift) / childViewControllerForStatusBarStyle (ObjC)。

建议

重写你类中的UINavigationController:

class MyRootNavigationController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    override var childForStatusBarStyle: UIViewController? {
        return nil
    }
}

非推荐替代方案

要为所有UINavigationController做这件事,你可以在一个扩展中覆盖(警告:它会影响UIDocumentPickerViewController, UIImagePickerController等),但你可能不应该根据Swift文档这样做:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    open override var childForStatusBarStyle: UIViewController? {
        return nil
    }
}

我的应用程序使用了所有三个:UINavigationController, UISplitViewController, UITabBarController,因此这些似乎都接管了对状态栏的控制,并将导致preferedStatusBarStyle不为他们的孩子被调用。要覆盖此行为,您可以创建一个扩展,就像前面提到的其他答案一样。这是一个扩展,这三个,在Swift 4。希望苹果对这类事情能更清楚些。

extension UINavigationController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
}

extension UITabBarController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.childViewControllers.first
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.childViewControllers.first
    }
}

extension UISplitViewController {
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return self.childViewControllers.first
    }

    open override var childViewControllerForStatusBarHidden: UIViewController? {
        return self.childViewControllers.first
    }
}

编辑:针对Swift 4.2 API更改的更新

extension UINavigationController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.topViewController
    }
}

extension UITabBarController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.children.first
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.children.first
    }
}

extension UISplitViewController {
    open override var childForStatusBarStyle: UIViewController? {
        return self.children.first
    }

    open override var childForStatusBarHidden: UIViewController? {
        return self.children.first
    }
}

Tyson的答案是正确的改变状态栏颜色为白色在UINavigationController。

如果有人想通过在AppDelegate中编写代码来实现相同的结果,那么请使用下面的代码并在AppDelegate的didFinishLaunchingWithOptions方法中编写它。

不要忘记在。plist文件中设置UIViewControllerBasedStatusBarAppearance为YES,否则更改将不会反映。

Code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // status bar appearance code
     [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

     return YES;
}

如果有人正在使用一个导航控制器,并希望他们所有的导航控制器都有黑色风格,你可以在Swift 3中写一个扩展到UINavigationController,它将应用于所有的导航控制器(而不是一次分配给一个控制器)。

extension UINavigationController {

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

        self.navigationBar.barStyle = UIBarStyle.black
    }

}