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


当前回答

对于任何使用UINavigationController的人:

UINavigationController不会转发preferredStatusBarStyle调用给它的子视图控制器。相反,它管理自己的状态——就像它应该做的那样,它在屏幕顶部绘制状态栏,因此应该对状态栏负责。因此,在导航控制器中实现preferredStatusBarStyle在vc中什么都不会做——它们永远不会被调用。

诀窍是UINavigationController使用什么来决定UIStatusBarStyleDefault或uistatusbarstyelightcontent返回什么。它基于它的UINavigationBar.barStyle。默认的(UIBarStyleDefault)导致前景UIStatusBarStyleDefault状态栏为黑色。UIBarStyleBlack会给出一个uistatusbarstyelightcontent状态栏。

TL; diana:

如果你想在UINavigationController上使用UIStatusBarStyleLightContent:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

其他回答

所以我实际上添加了一个category到UINavigationController,但是使用了这些方法:

-(UIViewController *)childViewControllerForStatusBarStyle;
-(UIViewController *)childViewControllerForStatusBarHidden;

让它们返回当前可见的UIViewController。这让当前可见视图控制器设置它自己的首选样式/可见性。

下面是它的完整代码片段:

迅速:

extension UINavigationController {

    public override func childViewControllerForStatusBarHidden() -> UIViewController? {
        return self.topViewController
    }

    public override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return self.topViewController
    }
}

在objective - c中:

@interface UINavigationController (StatusBarStyle)

@end

@implementation UINavigationController (StatusBarStyle)

-(UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

-(UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}

@end

为了更好地衡量,这是它如何在UIViewController中实现的:

在斯威夫特

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

override func prefersStatusBarHidden() -> Bool {
    return false
}

在objective - c中

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent; // your own style
}

- (BOOL)prefersStatusBarHidden {
    return NO; // your own visibility code
}

最后,确保你的app plist没有将“基于视图控制器的状态栏外观”设置为NO。要么删除这一行,要么将其设置为YES(我相信这是iOS 7的默认设置?)

对于任何使用UINavigationController的人:

UINavigationController不会转发preferredStatusBarStyle调用给它的子视图控制器。相反,它管理自己的状态——就像它应该做的那样,它在屏幕顶部绘制状态栏,因此应该对状态栏负责。因此,在导航控制器中实现preferredStatusBarStyle在vc中什么都不会做——它们永远不会被调用。

诀窍是UINavigationController使用什么来决定UIStatusBarStyleDefault或uistatusbarstyelightcontent返回什么。它基于它的UINavigationBar.barStyle。默认的(UIBarStyleDefault)导致前景UIStatusBarStyleDefault状态栏为黑色。UIBarStyleBlack会给出一个uistatusbarstyelightcontent状态栏。

TL; diana:

如果你想在UINavigationController上使用UIStatusBarStyleLightContent:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

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;
}

在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
    }
}

这是我解决这个问题的方法。

定义一个名为AGViewControllerAppearance的协议。

AGViewControllerAppearance.h

#import <Foundation/Foundation.h>

@protocol AGViewControllerAppearance <NSObject>

@optional

- (BOOL)showsStatusBar;
- (BOOL)animatesStatusBarVisibility;
- (UIStatusBarStyle)preferredStatusBarStyle;
- (UIStatusBarAnimation)prefferedStatusBarAnimation;

@end

在UIViewController上定义一个名为Upgrade的类别。

UIViewController+Upgrade.h

#import <UIKit/UIKit.h>

@interface UIViewController (Upgrade)

//
//  Replacements
//

- (void)upgradedViewWillAppear:(BOOL)animated;

@end

UIViewController+Upgrade.m

#import "UIViewController+Upgrade.h"

#import <objc/runtime.h>

#import "AGViewControllerAppearance.h" // This is the appearance protocol

@implementation UIViewController (Upgrade)

+ (void)load
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wselector"
    Method viewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
#pragma clang diagnostic pop
    Method upgradedViewWillAppear = class_getInstanceMethod(self, @selector(upgradedViewWillAppear:));
    method_exchangeImplementations(viewWillAppear, upgradedViewWillAppear);
}

#pragma mark - Implementation

- (void)upgradedViewWillAppear:(BOOL)animated
{
    //
    //  Call the original message (it may be a little confusing that we're
    //  calling the 'same' method, but we're actually calling the original one :) )
    //

    [self upgradedViewWillAppear:animated];

    //
    //  Implementation
    //

    if ([self conformsToProtocol:@protocol(AGViewControllerAppearance)])
    {
        UIViewController <AGViewControllerAppearance> *viewControllerConformingToAppearance =
        (UIViewController <AGViewControllerAppearance> *)self;

        //
        //  Status bar
        //

        if ([viewControllerConformingToAppearance respondsToSelector:@selector(preferredStatusBarStyle)])
        {
            BOOL shouldAnimate = YES;

            if ([viewControllerConformingToAppearance respondsToSelector:@selector(animatesStatusBarVisibility)])
            {
                shouldAnimate = [viewControllerConformingToAppearance animatesStatusBarVisibility];
            }

            [[UIApplication sharedApplication] setStatusBarStyle:[viewControllerConformingToAppearance preferredStatusBarStyle]
                                                        animated:shouldAnimate];
        }

        if ([viewControllerConformingToAppearance respondsToSelector:@selector(showsStatusBar)])
        {
            UIStatusBarAnimation animation = UIStatusBarAnimationSlide;

            if ([viewControllerConformingToAppearance respondsToSelector:@selector(prefferedStatusBarAnimation)])
            {
                animation = [viewControllerConformingToAppearance prefferedStatusBarAnimation];
            }

            [[UIApplication sharedApplication] setStatusBarHidden:(! [viewControllerConformingToAppearance showsStatusBar])
                                                    withAnimation:animation];
        }
    }
}

@end

现在,是时候说你的视图控制器正在实现AGViewControllerAppearance协议。

例子:

@interface XYSampleViewController () <AGViewControllerAppearance>

... the rest of the interface

@end

当然,你可以实现其余的方法(showsStatusBar, animatesStatusBarVisibility, prefereredstatusbaranimation)从协议和UIViewController+升级将做适当的 基于它们提供的值进行定制。