0

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

  • Status bar style = White
  • View controller-based status bar appearance = No

Based on this related question, Status Bar Text Color on iPhone XR is different, I tried

  • View controller-based status bar appearance = Yes

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

Saamer
  • 4,687
  • 1
  • 13
  • 55
John Mc
  • 212
  • 2
  • 16

3 Answers3

0

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;
}
Saamer
  • 4,687
  • 1
  • 13
  • 55
  • Sammer, in Xamarin.Forms iOS project we have AppDelegate.cs and Main.cs. Neither have that override and I don't know what a ViewController is in this context. – John Mc Jun 07 '19 at 15:50
  • Hey John, as I mentioned, you have to create a Custom Renderer. Can you also share what other devices & iOS versions are you seeing this issue. – Saamer Jun 07 '19 at 15:53
  • Sure, so I have physical iPad 5th gen, iPhone 8 and iPhone XR all running 12.2. Then I have all the simulators available to Visual Studio 2017 connected to MacBook Pro which runs Mojave. The XR real and sim are the ones with the issue. I have't tried all the other simulators. – John Mc Jun 07 '19 at 18:13
0

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

} `

Habin Lama
  • 529
  • 5
  • 19
0

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: enter image description here

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.

Ax1le
  • 6,563
  • 2
  • 14
  • 61