How to hide the status bar in an AVPlayerViewController? - ios

i wanna know if this can be done, i'm working on IOS 10, xCode 8 and swift 3, i tried various solutions from here but none works:
i tried to override the prefersStatusBarHidden, i tried to assign a false value but it's a get-only property and in appdelegate, i can't do this:
application.statusBarHidden = true
finally, i set in the plist the following:
Status bar is initially hidden to YES View
View controller-based status bar appearance to NO
and had no effect, i believe that all this solutions don't work because the upgrade to IOS 10.

Even after hiding the status bar for the entire app using:
application.isStatusBarHidden = true
AVPlayerViewController still showed the status bar. On going back to the presenting view controller (for which status bar was hidden earlier) status bar became visible. Tried to override prefersStatusBarHidden on both presenting and presented view controllers to no avail.
The only thing that worked was using deprecated method setStatusBarHidden in the presenting view controller's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIApplication.shared.setStatusBarHidden(true, with: .none)
}

You can hide the status bar in any or all of your view controllers just by adding this code:
override var prefersStatusBarHidden: Bool {
return true
}
Any view controller containing that code will hide the status bar by default.
If you want to animate the status bar in or out, just call setNeedsStatusBarAppearanceUpdate() on your view controller – that will force prefersStatusBarHidden to be read again, at which point you can return a different value. If you want, your call to setNeedsStatusBarAppearanceUpdate() can actually be inside an animation block, which causes the status bar to hide or show in a smooth way.
from: https://www.hackingwithswift.com/example-code/uikit/how-to-hide-the-status-bar

This work for me:
override var prefersStatusBarHidden: Bool {
get {
return true;
}
}

Simply subclass the AVPlayerViewController:
class PlayerViewController: AVPlayerViewController {
override var prefersStatusBarHidden: Bool {
return true
}
}
and use PlayerViewController()

This can be solved using extension of AVPlayerViewController:
Add the following line to AVPlayerViewController
extension AVPlayerViewController{
override open var prefersStatusBarHidden: Bool {
return true
}
}

Do this steps:
In info.plist file set
View controller-based status bar appearance = NO
in AppDelegate.swift file
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// use this code to hide status bar
application.isStatusBarHidden = true
return true
}
This codes enough to hide status bar in swift 3.

Related

prefersStatusBarHidden issue in iOS 13

Hi everyone I'm trying to hide my statusBar in a View Controller but it doesn't seem to work .. I used the function:
override var prefersStatusBarHidden: Bool {
         return true
    }
I also set the View controller-based status bar appearance in the plist file to YES
My status bar doesn't want to hide ... where am I doing wrong?
It looks like you're trying to specifically hide the status bar in a single ViewController.
In order to do that, you need have the following in that ViewController
self.modalPresentationCapturesStatusBarAppearance = true
override var prefersStatusBarHidden: Bool {
return true
}
I also added View controller-based status bar appearance in my .plist and set it to YES.
Tested on the latest iOS 13.
If the target view controller is embedded in another container view controller such as UINavigationController you need to subclass that container view controller and override its childForStatusBarHidden to return the target view controller.
you kan look this,you should override childForStatusBarHidden and childForStatusBarStyle 。
class CCNavigationController: UINavigationController {
override var childForStatusBarHidden: UIViewController? {
return self.topViewController
}
override var childForStatusBarStyle: UIViewController? {
return self.topViewController
}
}

Hide Status Bar In certain View controllers *Specific*

I have a very specific question concerning hiding status bar.
I incorporated the code below to easily get a status bar on every view controller, but I have a cameraViewcontroller where I don't want a status bar. I'm trying to hide the status bar.
//change the statusbar color for all view controllers
application.statusBarStyle = .lightContent
I tried using
override var prefersStatusBarHidden: Bool {
return true
}
and
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isStatusBarHidden = true
return true
}
but none of this seems to work. I'm new to programming and stack overflow so please help me out. I tried search for the answer but can't seem to find the answer to this particular dilemma. Your help will be much appreciated! Thank you.
its works for me perfectly.
Objective-C
-(BOOL)prefersStatusBarHidden{
return YES;
}
Swift
override var prefersStatusBarHidden: Bool {
return true
}
you should also use info.plist
"View controller-based status bar appearance" set to NO
"Status bar is initially hidden" set to YES
Can you apply below code in the CameraVC :
override var prefersStatusBarHidden: Bool {
return true
}

Hide status bar but not custom UIWindow

I have a custom UIWindow that is my own status bar, so I want to hide the status bar and show mine instead. I have
override var prefersStatusBarHidden: Bool {
return true
}
In both my UIWindow's rootViewController and the main ViewController. This hides the status bar, but it also hides my UIWindow. My UIWindow's windowLevel is UIWindowLevelStatusBar + 1, but I tested other windowLevels and it was hidden regardless of the level.
Setting an opaque background for my custom UIWindow is not an option.
How can I hide the status bar but not my custom UIWindow?
Edit: better solution
The same still holds true as before/below, but the actual code is shorter, simpler, and more Swifty.
In your AppDelegate, have
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
myStatusBarWindow.isHidden = false
return true
}
And, of course, hide the system status bar. Either do this in the build settings or info.plist, or in your view controller, say
override var prefersStatusBarHidden: Bool {
return true
}
Much better :)
Old solution
Unfortunately, I cannot find what is probably the best answer, but I have a workaround:
First show your window. Only after that should you hide the status bar. I am not sure why this works, but it does. You can do that like this:
// Class var
var statusBarHidden = false
// For example in viewDidLoad but only the order matters
override func viewDidLoad() {
myWindow.isHidden = false
statusBarHidden = true
setNeedsStatusBarAppearanceUpdate()
}
override var prefersStatusBarHidden: Bool {
return statusBarHidden
}
Through my experimentation I discovered that prefersStatusBarHidden is checked before viewDidLoad. Thus, we need to tell the view controller to check again once the window is shown. Again, I don't know why this works, but it does. If anyone can explain this phenomenon (why this order matters), that would be an excellent supplement to this solution.

Set color of text in status bar it is not working (swift 3 - iOS 10)

My status bar is black and I'm trying to set as white, so I did what I found in some questions here and put this on my AppDelegate...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Status bar color
UIApplication.shared.statusBarStyle = .lightContent
return true
}
There is no error in my console, nothing about this. The status bar text is still black. What could be causing this? There is another way to do that in swift 3.0?
ensure once in your project info.plist the row
View controller-based status bar appearance and set it to NO
In Swift3 you have to use following code, put anywhere in your view controller
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

How can I show/hide the status bar on a pageViewController using tap gesture (iOS8 / Swift)

Looking through all the solutions given to similar questions, I have been trying to get the statusBar to show/hide with a tap gesture.
I have set View controller-based status bar appearance = NO in the plist.
I have tried the following code in my DataViewController (page view controller) AND in the RootViewController:
let app = UIApplication.sharedApplication()
app.setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
and it doesn't work.
This is embedded in a UITabBarController, would that make a difference?
Also, I was able to get the following to hide the statusBar from the RootViewController:
override func prefersStatusBarHidden() -> Bool {
return true
}
But the DataViewController does not even call this function, and was only able to hide it permanently this way, rather than toggle it on/off.
Any ideas?
I have tried it in code, everything works fine for me. Make sure that the View controller-based status bar appearance is Set to NO. And there is no needs to override prefersStatusBarHidden().
if you using UIPageViewController then you should use this code in the RootViewController
if you have a navigationController it will hide it too
on ViewDidLoad()
self.navigationController?.hidesBarsOnTap = true
and use this method to hide or show the status Bar based on if the navigationBar is hidden or not
override func prefersStatusBarHidden() -> Bool {
if self.navigationController?.navigationBarHidden == true {
return true
}
else
{
return false
}
}

Resources