美文网首页iOS 问答集合iOS 开发
如何修改UIImagePickerController状态栏颜色

如何修改UIImagePickerController状态栏颜色

作者: 船长_ | 来源:发表于2018-07-07 19:47 被阅读36次

    应用场景: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;
    

    相关文章

      网友评论

        本文标题:如何修改UIImagePickerController状态栏颜色

        本文链接:https://www.haomeiwen.com/subject/hhjguftx.html