Xamarin.Forms - Change StatusBar Color

前端 未结 7 1604
小鲜肉
小鲜肉 2020-11-29 05:51

I search but I can\'t find if it\'s possible to change the StatusBar color for each platform, from my portable code? (for Android, iOS & WinPhone 8.1)

7条回答
  •  粉色の甜心
    2020-11-29 06:42

    I'm coming back to this answer years later to fix it because my answer had been wrong even though it had been accepted as the correct answer. I have now fixed it.

    I had misread the question to want to change the navigation bar or that it worked differently in Android at that time.

    I think at least this is a much better answer and should be better help to change the color of the navigationbar in Android and iOS.

    Add this code to your Xamarin.Forms project

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

    add this class to your Android project

    [assembly: Dependency(typeof(MyDemo.Droid.CustomRenderers.Statusbar))]
    namespace MyDemo.Droid.CustomRenderers
    {
        public class Statusbar : IStatusBarPlatformSpecific
        {
           public Statusbar()
           {
           }
    
           public void SetStatusBarColor(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
     CrossCurrentActivity.Current.Activity.Window.SetStatusBarColor(androidColor);
             }
             else
             {
              // Here you will just have to set your 
              // color in styles.xml file as shown below.
             }
           }
       }
    }
    

    Add this CurrentActivityPlugin to your projects

    Now you can change the color in your Forms project like this

    var statusbar = DependencyService.Get();
    statusbar.SetStatusBarColor(Color.Green);
    

    Note the else statement above. If you are using an older buildversion than 21 you will need to hard-code your color to the styles.xml in your Android project like this

    
    
       
    
    

    For iOS its similar

    add this to your Info.plist (more docs here)

    UIStatusBarStyle
    UIStatusBarStyleLightContent
    UIViewControllerBasedStatusBarAppearance
    
    

    and add this code to your SetStatusBarColor ethod in the iOS version of the StatusBar class

    UIView statusBar = UIApplication.SharedApplication.ValueForKey(
    new NSString("statusBar")) as UIView;
    
     if (statusBar != null && statusBar.RespondsToSelector(
     new  ObjCRuntime.Selector("setBackgroundColor:")))
     {
       // change to your desired color 
       statusBar.BackgroundColor = Color.FromHex("#7f6550").ToUIColor(); 
     }
    

    I wrote a more detailed blog post on how to set the color of the statusbar from Xamarin.Forms if somebody is interested. It does only talk about Android and iOS but should give you an idea what to do with other platforms.

提交回复
热议问题