Can't hide status bar in AVPlayerViewController's portrait mode
Just figured out a solution:
extension UINavigationController {
public override func childViewControllerForStatusBarHidden() -> UIViewController? {
return self.childViewControllers.last
}
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return self.childViewControllers.last
}
}
Yup, just override those two functions for all Navigation View Controllers in your app. I consider this to be a hack and not the 'correct' solution to this problem, so use this with caution.
As far as I can tell, this should never fail. In my app, every Navigation VC has exactly one child VC, but YMMV.
StatusBar not adhering to the app's orientation and rotating
I ended up removing the above code in my root view controller and adding this into my app delegate. Status bar now stays put.
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if let vc = window?.rootViewController?.presentedViewController {
if NSStringFromClass(vc.classForCoder) == "AVFullScreenViewController" {
return UIInterfaceOrientationMask.AllButUpsideDown
}
}
return UIInterfaceOrientationMask.Portrait
}
iOS 14 display status bar in landscape
Your code is correct (this is all you need in a new vanilla iOS app project):
override var prefersStatusBarHidden: Bool {
return false
}
The problem is that this works only up through iOS 12. In iOS 13 and later, the runtime stopped interpreting a value of false
to allow you to override the hiding of the status bar in landscape on an iPhone. Similarly, calling application.setStatusBarHidden
fails to show the status in landscape.
This is probably a side effect of the UIWindowScene architecture that was introduced in iOS 13. Or it might be intended to make all iPhone models behave the same way (with or without bezel).
How to force view controller orientation in iOS 8?
For iOS 7 - 10:
Objective-C:
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
Swift 3:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
Just call it in - viewDidAppear:
of the presented view controller.
iOS. How to enable and disable rotation on each UIViewController?
I have the answer.
On AppDelegate
, if you rotate device, push viewcontroller, etc. This function always call
Update for swift 3/4
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
return self.restrictRotation
}
self.restrictRotation
is a custom parameter.
How to use:
In Appdelegate:
var restrictRotation:UIInterfaceOrientationMask = .portrait
In ViewController:
When method ViewDidLoad or viewWillAppear is called. We will change like this:
(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all
and then this method on AppDelegate will be called.
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
Related Topics
Macos Change Menubar Icon Using Swift 4
Write Data to Firebase in The Background After Retrieving Steps with Healthkit's Background Delivery
Macos Swift UI View Where There Is a Search Field in The Title Bar
How to Apply Impulse to The Node on Touch Angle
How to Run Terminal Command in Swift from Any Directory
Exc_Badinstruction When Computing a Hash Value
Protocol Extension Doesn't Work with Rct_Export_View_Property
How to Make an Ellipse/Circular UIimage with Transparent Background
How to Set .Realm File on Realm
Closure (With Default Value) as Function Parameter
Swift Calendar Class Returning Wrong Date for Weekday