Reputation: 523
How to Change Status Bar Background, Text Color for Single Content Page.
I add:
styles.xml
I think it works on all Content Pages
I want:
Page1: Status Bar Background is Green.
Page2: Status Bar Background is Red. ...
Thank you!
Upvotes: 1
Views: 2443
Reputation: 1
on ios:
[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.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);
foreach (UIScene scene in UIApplication.SharedApplication.ConnectedScenes)
{
if (scene.ActivationState == UISceneActivationState.ForegroundActive)
{
UIWindowScene myScene = (UIWindowScene)scene;
foreach (UIWindow win in myScene.Windows)
{
if (win.IsKeyWindow)
{
win.AddSubview(statusBar);
}
}
}
}
}
}
}
Upvotes: 0
Reputation: 13853
How to Change Status Bar Background, Text Color for Single Content Page Xamarin (Ios, Android)
If you want to change the Status Bar Background color for single page, you can use DependencyService to achieve this.
1.In xamarin forms,add interface IStatusBarPlatformSpecific
public interface IStatusBarPlatformSpecific
{
void SetStatusBarColor(Color color);
}
2.In android,add class ChangeStatusbar
to implement interface IStatusBarPlatformSpecific
:
[assembly: Dependency(typeof(ChangeStatusbar))]
namespace ToastApp.Droid
{
public class ChangeStatusbar : IStatusBarPlatformSpecific
{
public ChangeStatusbar()
{
}
public void SetStatusBarColor(Xamarin.Forms.Color color)
{
// The SetStatusBarcolor is new since API 21
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var androidColor = color.AddLuminosity(-0.1).ToAndroid();
//Just use the plugin
Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(androidColor);
}
else
{
// Here you will just have to set your
// color in styles.xml file as shown below.
}
}
}
}
Note:Android only supports black or white icons & text.
For more, check:How to customize status bar icons and text color? E.g. status bar background: white, status bar icon color, and text: red
3.In IOS,create class ChangeStatusbarIOS
to implement interface IStatusBarPlatformSpecific
:
[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.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);
}
}
}
4.In forms, you can change status backbround color for Single Content Page by override method OnAppearing
and change back to it's original color by override method OnDisappearing
:
protected override void OnAppearing()
{
base.OnAppearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.FromHex("3F51B5"));
}
Upvotes: 3