I followed this thread to override -preferredStatusBarStyle
, but it isn't called.
Are there any options that I can change to enable it? (I'm using XIBs in my project.)
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
Possible root cause
I had the same problem, and figured out it was happening because I wasn't setting the root view controller in my application window.
The
UIViewController
in which I had implemented thepreferredStatusBarStyle
was used in aUITabBarController
, which controlled the appearance of the views on the screen.When I set the root view controller to point to this
UITabBarController
, the status bar changes started to work correctly, as expected (and thepreferredStatusBarStyle
method was getting called).Alternative method (Deprecated in iOS 9)
Alternatively, you can call one of the following methods, as appropriate, in each of your view controllers, depending on its background color, instead of having to use
setNeedsStatusBarAppearanceUpdate
:or
Note that you'll also need to set
UIViewControllerBasedStatusBarAppearance
toNO
in the plist file if you use this method.Swift 3 iOS 10 Solution:
On a UINavigationController,
preferredStatusBarStyle
is not called because itstopViewController
is preferred toself
. So, to getpreferredStatusBarStyle
called on an UINavigationController, you need to change itschildViewControllerForStatusBarStyle
.To do it for one UINavigationController (my recommendation):
To do it for all UINavigationController (warning: it affects UIDocumentPickerViewController, UIImagePickerController, etc.):
So I actually added a category to UINavigationController but used the methods:
and had those return the current visible UIViewController. That lets the current visible view controller set its own preferred style/visibility.
Here's a complete code snippet for it:
In Swift:
In Objective-C:
And for good measure, here's how it's implemented then in a UIViewController:
In Swift
In Objective-C
Finally, make sure your app plist does NOT have the "View controller-based status bar appearance" set to NO. Either delete that line or set it to YES (which I believe is the default now for iOS 7?)
The NavigationController or TabBarController are the ones that need to provide the style. Here is how I solved: https://stackoverflow.com/a/39072526/242769
In Swift for any kind of UIViewController:
In your
AppDelegate
set:myRootController
can be any kind ofUIViewController
, e.g.UITabBarController
orUINavigationController
.Then, override this root controller like this:
This will change the appearance of the status bar in your whole app, because the root controller is solely responsible for the status bar appearance.
Remember to set the property
View controller-based status bar appearance
to YES in yourInfo.plist
to make this work (which is the default).