0

I'm setting the SetStatusBarColor change on Xamarin. Everything works fine on Android. However on iOS I have the problem: System.NullReferenceException: 'Object reference not set to an instance of an object'

enter image description here

Here's what I did:

Page1.xaml.cs

protected override void OnAppearing()
    {
        base.OnAppearing();

        var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
        statusbar.SetStatusBarColor(Color.FromHex("00AA13"));

        var navigationPage = Application.Current.MainPage as NavigationPage;
        navigationPage.BarBackgroundColor = Color.FromHex("00AA13");
        navigationPage.BarTextColor = Color.FromHex("ffffff");
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
        statusbar.SetStatusBarColor(Color.FromHex("ffffff"));

        var navigationPage = Application.Current.MainPage as NavigationPage;
        navigationPage.BarBackgroundColor = Color.White;
        navigationPage.BarTextColor = Color.Black;
    }

Info.plist

    ....
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
    </dict>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>
    <key>FirebaseAppDelegateProxyEnabled</key>
    <string>No</string>
    <key>XSAppIconAssets</key>
    <string>Media.xcassets/AppIconsview.appiconset</string>

What did I go wrong? Please help. Thanks

Update

I add:

public interface IStatusBarPlatformSpecific
    {
        void SetStatusBarColor(Color color);
    }
}

In Project IOS t add class: ChangeStatusbarIOS

[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace Test.iOS
{
    public class ChangeStatusbarIOS : IStatusBarPlatformSpecific
    {
        public void SetStatusBarColor(Color color)
        {

            UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
            //statusBar.BackgroundColor = UIColor.Yellow;

            statusBar.BackgroundColor = color.AddLuminosity(-0.1).ToUIColor();
            UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
        }
    }
Chim Di Tru
  • 495
  • 4
  • 17
  • [here](https://stackoverflow.com/questions/37993741/xamarin-forms-change-statusbar-color) is same question with this – Amin Oct 21 '21 at 12:03
  • most likely `statusbar` is null because your `DependencyService.Get()` is failing on iOS – Jason Oct 21 '21 at 12:12
  • Yes, when i debug then var statusbar = null. While on Android I get the value. So do you have any solution for it? – Chim Di Tru Oct 21 '21 at 12:40
  • then figure out why `DependencyService.Get()` is failing – Jason Oct 21 '21 at 12:42
  • Did you try the solution in the link Amin provided ? And if possible could you provide the implementation of `IStatusBarPlatformSpecific` in iOS ? – ColeX Oct 22 '21 at 05:35
  • Yes, I have tried and it doesn't seem to work for me either. I have updated the above – Chim Di Tru Oct 22 '21 at 08:59
  • I follow the suggestion from here https://stackoverflow.com/questions/68801603/how-to-change-status-bar-background-text-color-for-single-content-page-xamarin – Chim Di Tru Oct 22 '21 at 09:04
  • I test your code on my side it works without problem , if possible could you provide us a basic, minimal project to reproduce the issue ? – ColeX Oct 25 '21 at 08:39
  • It doesn't seem to work from ios 13+, I'm testing on ios 14+ – Chim Di Tru Oct 26 '21 at 09:00
  • I test on iOS13,14,15 it works no problem , check the [screen shot](https://i.stack.imgur.com/Lfean.png). – ColeX Oct 27 '21 at 02:46

0 Answers0