The correct way to set a light status bar text color in iOS 7 based on different ViewControllers

后端 未结 8 1249
死守一世寂寞
死守一世寂寞 2020-12-25 14:52

I need to let a specific ViewController embedded in an UINavigationController to have light status bar text color (but other ViewControllers to beh

8条回答
  •  误落风尘
    2020-12-25 15:03

    Here's an improvement to Groot answer, in form of a simple category to UINavigationController, without the need to subclass UINavigationController.

    Swift

    extension UINavigationController {
        override public func preferredStatusBarStyle() -> UIStatusBarStyle {
            return topViewController?.preferredStatusBarStyle() ?? .Default
        }
    }
    

    Swift 3 & Swift 4

    extension UINavigationController {
        open override var preferredStatusBarStyle: UIStatusBarStyle {
            return topViewController?.preferredStatusBarStyle ?? .default
        }
    }
    

    Objective-C

    @implementation UINavigationController (StatusBarStyle)
    
    - (UIStatusBarStyle)preferredStatusBarStyle 
    {
        return [self.topViewController preferredStatusBarStyle];
    }
    
    @end
    

提交回复
热议问题