如何使UIImagePickerController StatusBar lightContent样式?
当我介绍ImagePickerController的时候,statusBar的文字颜色还是黑的,怎么样做呢?
只需三步:
1:添加UINavigationControllerDelegate
, UIImagePickerControllerDelegate
到你的
@interface yourController ()<>
2: imagePickerController.delegate = self;
3:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; }
我有同样的问题不得不pipe理在不同的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]; }
Swift解决scheme通过为UIImagePickerController编写扩展:
extension UIImagePickerController { convenience init(navigationBarStyle: UIBarStyle) { self.init() self.navigationBar.barStyle = navigationBarStyle } }
然后可以在初始化时设置颜色:
let picker = UIImagePickerController(navigationBarStyle: .black) // black bar -> white text
另一种select(由folse的答案启发):当你正常初始化UIImagePickerController,使这个类的委托( picker.delegate = self
),并实现此function:
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 } }
在Swift和iOS 9中, setStatusBarStyle
已被弃用。 你可以inheritance控制器。
private final class LightStatusImagePickerController: UIImagePickerController { override func preferredStatusBarStyle() -> UIStatusBarStyle { return .LightContent } }
使用上面的答案为我工作:
实现UINavigationControllerDelegate, UIImagePickerControllerDelegate
到你的UIViewController
并设置
imagePickerController.delegate = self;
添加以下方法:
-(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated { navigationController.navigationBar.barStyle = UIBarStyleBlack; }
这是我能想到的最快捷的解决scheme。 创build以下类别:
@implementation UIImagePickerController (LightStatusBar) - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } @end