iphoneX not call prefersStatusBarHidden
You need to check if your view controller is included in a container (i.e. UINavigationController). If that is the case, the full procedure is this:
1) Set the View controller-based status bar appearance
value in info.plist file to YES
2) In your child controller add this code:
override var prefersStatusBarHidden: Bool{
return true
}
3) Add this extension:
// gives control of the status bar appearance to the top controller
extension UINavigationController {
override open var childViewControllerForStatusBarHidden: UIViewController? {
return self.topViewController
}
}
You already have point 1 and 2. Same logic applies to UITabBarController
prefersStatusBarHidden iOS 13 not called
On an iPhone X-type device (anything without a bezel), Apple does not want you to hide the status bar, so your view controller's prefersStatusBarHidden
is not called.
You may be able to work around this by subclassing UINavigationViewController; in my testing, that worked, but I don't know whether Apple would approve.
prefersStatusBarHidden not called
Figured it out. in info.plist file:
view controller-status bar appearance should be set to YES
prefersStatusBarHidden issue in iOS 13
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.
Correct way of hiding StatusBar in SwiftUI
Example 1
Each controller is able to hide / show the bar individually. However, if you write a generic extension for all view controllers, this basically means you are switching it off for all views. This is actually not different from switching it off entirely for the whole app via Info.plist
// switch off statusbar for the entire app (all views)
extension UIViewController {
func prefersStatusBarHidden() -> Bool {
return true
}
}
// switch off statusbar for only specific views
class MyViewController: UIViewController {
..
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Example 2
You need to know if your ViewController is included in a container (such as UINavigationController) in that case the NavigationController takes control of the StatusBar. You might to write a solution where the navigation controller always gives the control to the topviewcontroller in this case: see iphoneX not call prefersStatusBarHidden
NavigationView {
}
.statusBar(hidden: true)
Example 4
You can set the status also once for the whole app. That is done in the Info.plist file
<key>UIStatusBarHidden</key>
<true/>
Example 5
You can setup the setting for the whole app but have it differently for each target. Thats done here.
Status Bar Style: Hide status bar (in Target Settings)
Status bar not animating in iPhone X
I got your code to work nicely, as long as I did not have the first view controller embedded in an UINavigationController.
If you have your first view controller in an UINavigationController:
Make a subclass, f.ex CustomNavigationController:
class CustomNavigationController: UINavigationController {
var isStatusBarHidden: Bool = false
override var prefersStatusBarHidden: Bool {
return isStatusBarHidden
}
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
}
Then in the prepare for segue method in the first view controller:
if let navigationController = navigationController as? CustomNavigationController {
navigationController.isStatusBarHidden = true
UIView.animate(withDuration: 0.5, animations: {
self.setNeedsStatusBarAppearanceUpdate()
})
}
And as you have in your second view controller:
override var prefersStatusBarHidden: Bool {
return true
}
Then it animated nicely for me on the iPhone X.
prefersStatusBarHidden and Black bar in window after backgrounding an iPhone only app on iPad iOS 8
I fixed my issues by...
Setting "View controller-based status bar appearance" to NO.
Testing for iOS version and setting my NavBar background image accordingly...
NSString *version = [[UIDevice currentDevice] systemVersion];
bool isAtLeastiOS_8 = [version floatValue] >= 8.0;
if(isAtLeastiOS_8) {
UIImage *bgImagePortrait = [UIImage imageNamed:@"NavBarPortrait.png"];
[self.navigationBar setBackgroundImage:bgImagePortrait forBarMetrics:UIBarMetricsDefault];
} else {
UIImage *bgImagePortrait = [UIImage imageNamed:@"NavBarPortrait.png"];
UIImage *bgImageLandscape = [UIImage imageNamed:@"NavBarLandscape.png"];
[[UINavigationBar appearance] setBackgroundImage:bgImagePortrait
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:bgImageLandscape
forBarMetrics:UIBarMetricsLandscapePhone];
}- Accepting the loss of the status bar in landscape mode on iOS 8
Detect screen notch from prefersStatusBarHidden
I'm not familiar with Obj-c but that looks like a computed property/function. Every time you access it, it will get the current safe area inset and return a Bool.
But the problem is that you are then setting prefersStatusBarHidden
based on that Bool. If the status bar is hidden, the safe area will get smaller. Then, the next time you access the hasTopNotch
property, it will return an incorrect value.
Instead, what I do is check the safe area once and only once the app starts. Your user's device isn't ever going to change, so you don't need a function. In Swift:
var deviceHasNotch = false /// outside any class
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
deviceHasNotch = window?.safeAreaInsets.bottom ?? 0 > 0 /// set it here
}
}
Related Topics
Wrapping a Generic Method in a Class Extension
How to Cast a Uint64 to an Int64
Difference Between Nsrange and Nsmakerange
How to Read a Property List from Data in Swift 3
Gcd with Static Functions of a Struct
How to Set an Environment Object in Preview
Swift Enum Raw Value: Not Working with Cgfloat = -1.0
Google API - Invalid Credentials
How to Make Swiftui Listmenu with Different Behaviors
How to Change Navigationbar Font in Swift
Add Skreferencenode/Skscene to Another Skscene in Spritekit
Swiftui: How to Make a Button Open a Url in Safari
Making Parts of Text Bold in Swiftui
Iphonex Not Call Prefersstatusbarhidden
How to Mimic 'Uitableviewcontroller' Showing of the Large Titles in 'Navigationbar' on iOS 11
Cannot Invoke 'Filter' with an Argument List of Type '((_) -> _)'
Most Efficient Way to Access Multi-Dimensional Arrays in Swift