如何让UIImagePickerController的StatusBar呈现lightContent样式?

9
当我呈现ImagePickerController时,状态栏文本颜色仍然是黑色,如何使其变成这样?
7个回答

42

只需要三个步骤:

1:在您的代码中添加UINavigationControllerDelegateUIImagePickerControllerDelegate

@interface yourController ()<>

2: imagePickerController.delegate = self;

3:

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

1
它能够工作是因为 UIImagePickerControllerUINavigationController 的子类,而该委托方法在每次您在导航控制器堆栈上呈现另一个视图控制器时都会被调用。 - barndog
4
setStatusBarStyle 在 iOS 9 中已被弃用。 - Huy Le
@barndog,当imagePicker显示时,我该如何隐藏状态栏?由于状态栏没有被裁剪掉,所以存在一个bug,即裁剪后的图像会添加y偏移量,其大小与状态栏相同。 - AnonProgrammer

4

通过为UIImagePickerController编写扩展的方式,提供了Swift解决方案:

extension UIImagePickerController {
    convenience init(navigationBarStyle: UIBarStyle) {
        self.init()
        self.navigationBar.barStyle = navigationBarStyle
    }
}

然后您可以在初始化时设置颜色:
let picker = UIImagePickerController(navigationBarStyle: .black)    // black bar -> white text

替代方案(受folse's answer启发):当您正常初始化UIImagePickerController时,将此类设置为委托(picker.delegate = self),并实现此函数:

func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
    if navigationController is UIImagePickerController {        // check just to be safe
        navigationController.navigationBar.barStyle = .black    // black bar -> white text
    }
}

第一种解决方案(自定义初始化程序)对我不起作用。 - Cesare
问题是关于状态栏颜色而不是导航栏颜色。 - Igor Kulagin

3

在Swift和iOS 9中,setStatusBarStyle已被弃用。你可以对控制器进行子类化。

private final class LightStatusImagePickerController: UIImagePickerController {
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .lightContent
    }
}

很好的建议,我认为这是前进的正确方式!但出于某种原因,它对我没有起作用。调用已被调用,我也尝试了prefersStatusBarHidden,并在viewDidLoad中调用了self.setNeedsStatusBarAppearanceUpdate。状态栏仍然显示为darkContent。 - shashwat
根据苹果的UIImagePickerController文档,您不应该对控制器进行子类化。 "UIImagePickerController类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不能修改,但有一个例外。您可以将自定义视图分配给cameraOverlayView属性,并使用该视图呈现其他信息或管理相机界面与您的代码之间的交互。" - matrejek
它在iOS 13中不起作用。子类的方法没有被调用。 - Igor Kulagin

2

根据上面的答案,以下方法对我有效:

UINavigationControllerDelegate, UIImagePickerControllerDelegate 实现到你的 UIViewController 中,并设置

imagePickerController.delegate = self;

添加以下方法:
-(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated { 
    navigationController.navigationBar.barStyle = UIBarStyleBlack;
}

2

我也遇到了同样的问题,需要管理在不同的iOS版本下运行的应用程序。

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

if(IS_IOS8_AND_UP) {
    imagePickerController.modalPresentationStyle = UIModalPresentationFullScreen;
} else {
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
}

imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];

代理模式中的委托:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    /* Cancel button color  */
    _imagePicker.navigationBar.tintColor = <custom_color>
    /* Status bar color */
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

1
我曾遇到类似的问题,发现最简洁的解决方法是在UIImagePickerController的扩展中覆盖preferredStatusBarStyle。这个原则可以很好地应用于第三方库。
extension UIImagePickerController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        if isLightTheme() {
            return .default // black text
        }
        return .lightContent // white text
    }
}

isLightTheme() 是一个函数,用于确定该控制器中的 NavigationBar 是浅色还是深色。


0

这是我能想到的最快解决方案。创建以下类别:

@implementation UIImagePickerController (LightStatusBar)

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接