How to make UIImagePickerController StatusBar lightContent style?

前端 未结 7 1621
悲哀的现实
悲哀的现实 2020-12-29 12:53

\"enter

When I present the ImagePickerController the statusBar text color is still bla

相关标签:
7条回答
  • 2020-12-29 13:28

    I had the same problem having to manage the application runned under different iOS versions.

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

    The, in delegate:

    - (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];
    }
    
    0 讨论(0)
  • 2020-12-29 13:29

    I was facing a similar problem and I found the cleanest way to solve it was to override preferredStatusBarStyle in an extension of UIImagePickerController like so. This principal can be applied to third party libraries nicely.

    extension UIImagePickerController {
        open override var preferredStatusBarStyle: UIStatusBarStyle {
            if isLightTheme() {
                return .default // black text
            }
            return .lightContent // white text
        }
    }
    

    isLightTheme() is simply a function to determine whether the NavigationBar in that controller is a light or dark colour.

    0 讨论(0)
  • 2020-12-29 13:37

    Just three steps:

    1: Add UINavigationControllerDelegate,UIImagePickerControllerDelegate to your

    @interface yourController ()<>
    

    2: imagePickerController.delegate = self;

    3:

    -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }
    
    0 讨论(0)
  • 2020-12-29 13:44

    This is the quickest solution I could think of. Create the following category:

    @implementation UIImagePickerController (LightStatusBar)
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        return UIStatusBarStyleLightContent;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-29 13:46

    Using the answers above the following worked for me:

    Implement UINavigationControllerDelegate, UIImagePickerControllerDelegate to your UIViewController and set

    imagePickerController.delegate = self;
    

    Add the following method:

    -(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated { 
        navigationController.navigationBar.barStyle = UIBarStyleBlack;
    }
    
    0 讨论(0)
  • 2020-12-29 13:52

    In Swift and iOS 9, setStatusBarStyle is deprecated. You could subclass the controller.

    private final class LightStatusImagePickerController: UIImagePickerController {
        override func preferredStatusBarStyle() -> UIStatusBarStyle {
            return .lightContent
        }
    }
    
    0 讨论(0)
提交回复
热议问题