Using the techniques out there in googleland, I have set my iOS statusbar text to white. This works fine except for iPhone XR, which sets the text black.
The techniques I find for dealing with this are all Swift and Objective C related. What is the technique for Xamarin.Forms?
In plist I have
Based on this related question, Status Bar Text Color on iPhone XR is different , I tried
But that turns all iOS devices status bar to black, including XR. Then it talks about some Swift code for which I don't know the analog in Xamarin.
Based on this related question, https://forums.xamarin.com/discussion/89840/change-status-bar-color-on-ios
It is close but it changes the bar's background color. I can't find any property on the statusBar object that talks about text color.
Based on this related question, https://forums.xamarin.com/discussion/17922/navigationpage-statusbar-color
Using SetStatusBarStyle doesn't affect XR either.
Note: I'm not using NavigationPage
You're pretty close. It seems like you will need to use a custom renderer for this. In the ViewController, you will override the PreferredStatusBarStyle function to any of the three enums shown here as follows:
public override UIStatusBarStyle PreferredStatusBarStyle()
{
return UIStatusBarStyle.LightContent;
}
Swift 4.2 solution with NavigationController
First Step:
Open your info.plist and insert a new key named "View controller-based status bar appearance" or UIViewControllerBasedStatusBarAppearance to YES to let each VC use their own status property.
Second Step
In each VC, override the preferredStatusBarStyle property like this :
override var preferredStatusBarStyle : UIStatusBarStyle { return .lightContent //.default for black style }
Last step
Override the preferredStatusBarStyle property in your custom NavigationController class :
`class NavigationController : UINavigationController {
override var preferredStatusBarStyle : UIStatusBarStyle {
if let topVC = viewControllers.last {
//return the status property of each VC, look at step 2
return topVC.preferredStatusBarStyle
}
return .default
} `
I know we should not use UIStatusBarStyle
now. But it does work on iOS 12.2 on my XR simulator. I added the keys in my info.plist:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
The status bar's text changes to the white:
Using code to change the color also works fine:
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
You can utilize it to adjust the status bar style dynamically.
However, creating a custom renderer for your specified page is another option. Firstly, set the UIViewControllerBasedStatusBarAppearance
to true in the info.plist.
Then the page renderer could be like:
[assembly: ExportRenderer(typeof(MainPage), typeof(CustomPageRenderer))]
namespace App.iOS
{
public class CustomPageRenderer : PageRenderer
{
public override UIStatusBarStyle PreferredStatusBarStyle()
{
return UIStatusBarStyle.LightContent;
}
}
}
MainPage
is a content page class on Forms. And I set it to the App's MainPage
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.