问题描述
在我的应用程序中,您可以点击 UIButton
,弹出一个弹出窗口并显示用户的照片库。
In my app you can tap a UIButton
and a popover pops up and shows the Photo Library of the user.
我正在使用此代码来实现这一目标:
I am using this code to make that happen:
let picker = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .PhotoLibrary
picker.modalPresentationStyle = .Popover
self.presentViewController(picker, animated: true, completion: nil)
picker.popoverPresentationController?.sourceRect = CGRectMake(600,180,0,0)
picker.popoverPresentationController?.sourceView = self.view
上面的代码工作正常,并向用户显示弹出窗口。但是,它还会显示一个白色状态栏。
The code above works fine and does shows the popover to the user. However, it also shows a white status bar.
我尝试以多种方式隐藏状态栏:
I tried hiding the status bar in many ways:
UIApplication.sharedApplication().statusBarHidden = true
我确保info.plist文件中的 String
设置为否
:查看基于控制器的状态栏外观
。
I made sure this String
in the info.plist file was set to NO
: View controller-based status bar appearance
.
我的课程中也有此代码:
I also have this code in my class:
override func prefersStatusBarHidden() -> Bool {
return true
}
UIViewController
属性Inspector如下所示:
The UIViewController
Attributes Inspector looks like this:
不幸的是,popover仍显示状态栏。为什么?我该如何解决这个问题?
Unluckily, the popover still shows the status bar. Why? How can I fix that?
推荐答案
无论您的应用设置如何,ImagePickerController都非常热衷于显示状态栏。我设法通过继承ImagePickerController并覆盖 viewWillAppear
和 prefersStatusBarHidden
:
The ImagePickerController is very keen on displaying a status bar, regardless of your app settings. I have managed to surpress it by subclassing ImagePickerController and overriding viewWillAppear
and prefersStatusBarHidden
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
如果你的imagePicker sourceType
是 .SavedPhotosAlbum
但如果 sourceType
是 .PhotoLibrary
。后一种源类型为您提供了imagePickerController中的导航选项。当第一个屏幕的状态栏在您的控制之下时,只要您导航到 Moments 或 Camera Roll ,就会失去该控件。状态栏重新出现 - 更糟糕的是 - viewControllers之间的动画过渡真的搞砸了。您可以通过拦截UINavigationController委托方法来获得对该过程的更多控制(UIImagePickerController是UINavigationController的子类),但我只能成功地使用 soureType = .SavedPhotosAlbum
This solution is fine if your imagePicker sourceType
is .SavedPhotosAlbum
but doesn't work so well if sourceType
is .PhotoLibrary
. The latter source type presents you with navigation options within the imagePickerController. While the first screen's status bar is under your control, you lose that control as soon as you navigate to Moments or Camera Roll. The status bar reappears and - worse - animation transitions between the viewControllers are really messed up. You can get more control over the process by intercepting UINavigationController delegate methods (UIImagePickerController is a subclass of UINavigationController), but I have only succeeded well with soureType = .SavedPhotosAlbum
编辑
您可能还需要包含此内容:
you may also have to include this:
override func childViewControllerForStatusBarHidden() -> UIViewController? {
return nil;
}
出于完全神秘的原因!
EDIT2
全部放在一起...
class MyImagePickerController: UIImagePickerController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func childViewControllerForStatusBarHidden() -> UIViewController? {
return nil;
}
}
然后你改变这一行: / p>
Then you change this line:
let picker = UIImagePickerController()
to:
let picker = MyImagePickerController()
这篇关于为什么在Swift中调用popoverPresentationController时会出现状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!