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'
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);
}
}