【发布时间】:2014-02-09 03:34:56
【问题描述】:
当我展示ImagePickerController时,statusBar文字颜色还是黑色,怎么弄成这样?
【问题讨论】:
标签: ios objective-c uiimagepickercontroller statusbar
当我展示ImagePickerController时,statusBar文字颜色还是黑色,怎么弄成这样?
【问题讨论】:
标签: ios objective-c uiimagepickercontroller 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];
}
【讨论】:
UIImagePickerController 是UINavigationController 的子类,并且每次您在导航控制器堆栈上呈现另一个视图控制器时都会调用该委托方法。
setStatusBarStyle 在 iOS 9 中已弃用
通过为 UIImagePickerController 编写扩展的 Swift 解决方案:
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
}
}
【讨论】:
在 Swift 和 iOS 9 中,setStatusBarStyle 已弃用。你可以子类化控制器。
private final class LightStatusImagePickerController: UIImagePickerController {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .lightContent
}
}
【讨论】:
我在管理在不同 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];
}
【讨论】:
使用上面的答案对我有用:
实施
UINavigationControllerDelegate, UIImagePickerControllerDelegate 给你的UIViewController
并设置
imagePickerController.delegate = self;
添加如下方法:
-(void) navigationController: (UINavigationController *) navigationController willShowViewController: (UIViewController *) viewController animated: (BOOL) animated {
navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
【讨论】:
我遇到了类似的问题,我发现解决它的最简单方法是在 UIImagePickerController 的扩展中覆盖 preferredStatusBarStyle,就像这样。该主体可以很好地应用于第三方库。
extension UIImagePickerController {
open override var preferredStatusBarStyle: UIStatusBarStyle {
if isLightTheme() {
return .default // black text
}
return .lightContent // white text
}
}
isLightTheme() 只是一个函数,用于确定该控制器中的 NavigationBar 是浅色还是深色。
【讨论】:
这是我能想到的最快的解决方案。创建以下类别:
@implementation UIImagePickerController (LightStatusBar)
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
【讨论】: