Xamarin Forms - 更改状态栏颜色跨平台
Xamarin Forms - Change Statusbar Color crossplatform
我有一个 xamarin 表单应用程序,我已经能够更改导航栏颜色。如何跨平台更改状态栏颜色?在下图中,您可以看到绿色的导航页面栏背景颜色。上面是蓝色的,我想改变它的颜色。如何以 xamarin 形式实现此跨平台?
据我所知,您需要分别在每个平台上设置状态栏颜色。
Whosebug 和 Google 上有很多这样的问题可以帮助您。
对于Android:
在 Resources -> values 检查你的 styles.xml
寻找类似 <item name="android:statusBarColor">#000000</item>
的东西来设置颜色
对于iOS:
在您的 AppDelegate.cs 中查找 FinishedLaunsching
-方法。
您可以使用 UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
更改样式
你可以使用 DependencyService .
在共享项目中,定义接口
public interface IStatusBarColor
{
void SetColoredStatusBar(string color);
}
在Android
首先,从 nuegt 安装插件 CurrentActivity,检查 https://github.com/jamesmontemagno/CurrentActivityPlugin
using Android.OS;
using Android.Views;
using App24.Droid;
using App24;
using Xamarin.Forms;
using Plugin.CurrentActivity;
[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.Droid
{
public class SetStatusBarColorImplemention : IStatusBarColor
{
public SetStatusBarColorImplemention()
{
}
public void SetColoredStatusBar(string color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.DecorView.SystemUiVisibility = 0;
currentWindow.SetStatusBarColor(Android.Graphics.Color.ParseColor(color));
});
}
}
Window GetCurrentWindow()
{
var window = CrossCurrentActivity.Current.Activity.Window;
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
return window;
}
}
}
在iOS
using App24;
using App24.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
using CoreGraphics;
[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.iOS
{
public class SetStatusBarColorImplemention : IStatusBarColor
{
public void SetColoredStatusBar(string hexColor)
{
if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIView view = new UIView(window.WindowScene.StatusBarManager.StatusBarFrame);
window.AddSubview(view);
Device.BeginInvokeOnMainThread(() =>
{
if (view.RespondsToSelector(new Selector("setBackgroundColor:")))
{
view.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
});
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
});
}
}
UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
{
if (rootViewController is UITabBarController)
{
UITabBarController tabBarController = (UITabBarController)rootViewController;
return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
}
else if (rootViewController is UINavigationController)
{
UINavigationController navigationController = (UINavigationController)rootViewController;
return topViewControllerWithRootViewController(navigationController.VisibleViewController);
}
else if (rootViewController.PresentedViewController != null)
{
UIViewController presentedViewController = rootViewController.PresentedViewController;
return topViewControllerWithRootViewController(presentedViewController);
}
else
{
return rootViewController;
}
}
}
}
现在根据需要调用该行。
DependencyService.Get<IStatusBarColor>().SetColoredStatusBar("#00ff00"); // set the color of bar as green
我有一个 xamarin 表单应用程序,我已经能够更改导航栏颜色。如何跨平台更改状态栏颜色?在下图中,您可以看到绿色的导航页面栏背景颜色。上面是蓝色的,我想改变它的颜色。如何以 xamarin 形式实现此跨平台?
据我所知,您需要分别在每个平台上设置状态栏颜色。 Whosebug 和 Google 上有很多这样的问题可以帮助您。
对于Android:
在 Resources -> values 检查你的 styles.xml
寻找类似 <item name="android:statusBarColor">#000000</item>
的东西来设置颜色
对于iOS:
在您的 AppDelegate.cs 中查找 FinishedLaunsching
-方法。
您可以使用 UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);
你可以使用 DependencyService .
在共享项目中,定义接口
public interface IStatusBarColor
{
void SetColoredStatusBar(string color);
}
在Android
首先,从 nuegt 安装插件 CurrentActivity,检查 https://github.com/jamesmontemagno/CurrentActivityPlugin
using Android.OS;
using Android.Views;
using App24.Droid;
using App24;
using Xamarin.Forms;
using Plugin.CurrentActivity;
[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.Droid
{
public class SetStatusBarColorImplemention : IStatusBarColor
{
public SetStatusBarColorImplemention()
{
}
public void SetColoredStatusBar(string color)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
Device.BeginInvokeOnMainThread(() =>
{
var currentWindow = GetCurrentWindow();
currentWindow.DecorView.SystemUiVisibility = 0;
currentWindow.SetStatusBarColor(Android.Graphics.Color.ParseColor(color));
});
}
}
Window GetCurrentWindow()
{
var window = CrossCurrentActivity.Current.Activity.Window;
window.ClearFlags(WindowManagerFlags.TranslucentStatus);
window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
return window;
}
}
}
在iOS
using App24;
using App24.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
using CoreGraphics;
[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.iOS
{
public class SetStatusBarColorImplemention : IStatusBarColor
{
public void SetColoredStatusBar(string hexColor)
{
if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIView view = new UIView(window.WindowScene.StatusBarManager.StatusBarFrame);
window.AddSubview(view);
Device.BeginInvokeOnMainThread(() =>
{
if (view.RespondsToSelector(new Selector("setBackgroundColor:")))
{
view.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
});
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
}
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
});
}
}
UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
{
if (rootViewController is UITabBarController)
{
UITabBarController tabBarController = (UITabBarController)rootViewController;
return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
}
else if (rootViewController is UINavigationController)
{
UINavigationController navigationController = (UINavigationController)rootViewController;
return topViewControllerWithRootViewController(navigationController.VisibleViewController);
}
else if (rootViewController.PresentedViewController != null)
{
UIViewController presentedViewController = rootViewController.PresentedViewController;
return topViewControllerWithRootViewController(presentedViewController);
}
else
{
return rootViewController;
}
}
}
}
现在根据需要调用该行。
DependencyService.Get<IStatusBarColor>().SetColoredStatusBar("#00ff00"); // set the color of bar as green