当我呈现ImagePickerController时,statusBar文本颜色仍为黑色,如何制作这样的颜色?
答案 0 :(得分:42)
只需三步:
1:将UINavigationControllerDelegate
,UIImagePickerControllerDelegate
添加到您的
@interface yourController ()<>
2:imagePickerController.delegate = self;
3:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
答案 1 :(得分:4)
通过为UIImagePickerController编写扩展来快速解决方案:
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
}
}
答案 2 :(得分:3)
在Swift和iOS 9中,不推荐使用setStatusBarStyle
。你可以继承控制器。
private final class LightStatusImagePickerController: UIImagePickerController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .lightContent
}
}
答案 3 :(得分: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];
}
答案 4 :(得分:2)
使用上面的答案,以下内容对我有用:
实施
UINavigationControllerDelegate, UIImagePickerControllerDelegate
UIViewController
imagePickerController.delegate = self;
并设置
-(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated {
navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
添加以下方法:
h5py
答案 5 :(得分:1)
我遇到了类似的问题,发现解决此问题的最干净方法是像这样在preferredStatusBarStyle
的扩展名中覆盖UIImagePickerController
。该主体可以很好地应用于第三方库。
extension UIImagePickerController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if isLightTheme() {
return .default // black text
}
return .lightContent // white text
}
}
isLightTheme()
只是确定该控制器中NavigationBar是浅色还是深色的函数。
答案 6 :(得分:0)
这是我能想到的最快的解决方案。创建以下类别:
@implementation UIImagePickerController (LightStatusBar)
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end