您的当前位置:首页正文

如何修改UIImagePickerController状态栏颜色

来源:要发发知识网

应用场景:Swift4.1和Objective-C混编,Info.plist设置 View controller-based status bar appearance=YES下,系统默认是UIStatusBarStyleDefault

需求:想要设置成UIStatusBarStyleLightContent

一般在需要修改状态栏的控制器设置

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

调用系统相册例外
解决方法一:自定义个类DXUIImagePickerController,继承UIImagePickerController,在自定义类里设置

@interface DXUIImagePickerController ()

@end

@implementation DXUIImagePickerController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
@end

解决方法二:给UIImagePickerController扩展分类,在分类里设置

extension UIImagePickerController {
    // 无效
   open override var prefersStatusBarHidden: Bool{
        return true
    }
     // 无效
    override open var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
}

正确解法1

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate      = self;
picker.sourceType    = type;
picker.allowsEditing = YES;
// iOS10和iOS11上,状态栏颜色是白色的,如果不设置状态栏就是黑色的(亲测)
// picker.modalPresentationStyle UIModalPresentationOverCurrentContext;
picker.navigationBar.barTintColor self.navigationController.navigationBar.barTintColor;
[self presentViewController:picker animated:YES completion:^{
     [self setNeedsStatusBarAppearanceUpdate];
 }];

这样设置也是无效的

picker.modalPresentationStyle = UIModalPresentationCurrentContext;