Forum Xamarin.iOS

UIBarButtonItem.Appearance doesn't works on IOS 13

FeduniakVFeduniakV Member ✭✭
edited January 2020 in Xamarin.iOS

Hi!
I use navigation bar and right buttons on it. Before IOS 13, I had settings for those buttons in AppDelegate:

          var attr = UIBarButtonItem.Appearance.GetTitleTextAttributes(UIControlState.Application);
                attr.Font = UIFont.SystemFontOfSize(15);
                attr.TextColor = Color.FromHex("#2196F3").ToUIColor();
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Normal);
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Highlighted);

But on new IOS it didn't work. Only way that I find it's write custom render for NavigationPage - but it works only on first page, when I use new NavigationPage(). I need change this style on every place. Please tell me if exist similar way as AppDelegate.``

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    UIBarButtonItem.Appearance does not work after iOS13 any more , you could use UINavigationBarAppearance instead .

    Try the following code

          if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var style = new UINavigationBarAppearance();
                style.ConfigureWithTransparentBackground();
    
    
                var a = new UIStringAttributes();
                a.Font = UIFont.SystemFontOfSize(15);
                a.ForegroundColor = Color.FromHex("#2196F3").ToUIColor();
                style.TitleTextAttributes = a;
    
                var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
                button.Normal.TitleTextAttributes = a.Dictionary as NSDictionary<NSString, NSObject>;
                button.Highlighted.TitleTextAttributes = a.Dictionary as NSDictionary<NSString, NSObject>;
                style.ButtonAppearance = button;
    
                UINavigationBar.Appearance.StandardAppearance = style;           
            }
            else
            {
                var attr = UIBarButtonItem.Appearance.GetTitleTextAttributes(UIControlState.Application);
                attr.Font = UIFont.SystemFontOfSize(15);
                attr.TextColor = Color.FromHex("#2196F3").ToUIColor();
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Normal);
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Highlighted);
            }
    

    Refer

    https://stackoverflow.com/a/58082187/8187800
    https://stackoverflow.com/a/57990366/8187800

  • FeduniakVFeduniakV Member ✭✭

    @ColeX said:
    UIBarButtonItem.Appearance does not work after iOS13 any more , you could use UINavigationBarAppearance instead .

    Try the following code

          if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var style = new UINavigationBarAppearance();
                style.ConfigureWithTransparentBackground();
    
    
                var a = new UIStringAttributes();
                a.Font = UIFont.SystemFontOfSize(15);
                a.ForegroundColor = Color.FromHex("#2196F3").ToUIColor();
                style.TitleTextAttributes = a;
    
                var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
                button.Normal.TitleTextAttributes = a.Dictionary as NSDictionary<NSString, NSObject>;
                button.Highlighted.TitleTextAttributes = a.Dictionary as NSDictionary<NSString, NSObject>;
                style.ButtonAppearance = button;
    
                UINavigationBar.Appearance.StandardAppearance = style;           
            }
            else
            {
                var attr = UIBarButtonItem.Appearance.GetTitleTextAttributes(UIControlState.Application);
                attr.Font = UIFont.SystemFontOfSize(15);
                attr.TextColor = Color.FromHex("#2196F3").ToUIColor();
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Normal);
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Highlighted);
            }
    

    Refer

    https://stackoverflow.com/a/58082187/8187800
    https://stackoverflow.com/a/57990366/8187800

    I've got errors

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 2020

    Did you test on iOS 13 ? The error shows the class can't be accessible.

  • FeduniakVFeduniakV Member ✭✭
    edited January 2020

    @ColeX said:
    Did you test on iOS 13 ? The error shows the class can't be accessible.

    I updated XCode and VS. Now I get Exception:

  • FeduniakVFeduniakV Member ✭✭

    @ColeX said:
    Try the following code , it works fine on my side .

         if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var style = new UINavigationBarAppearance();
    
                var a = new UIStringAttributes();
                a.Font = UIFont.SystemFontOfSize(35);
                a.ForegroundColor = UIColor.Yellow;
                style.TitleTextAttributes = a;
    
                var dic = new NSDictionary<NSString, NSObject>(
                    new NSString[] {a.Dictionary.Keys[0] as NSString, a.Dictionary.Keys[1] as NSString }, a.Dictionary.Values
                    );
           
                var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
                button.Normal.TitleTextAttributes = dic;
                button.Highlighted.TitleTextAttributes = dic;
                style.ButtonAppearance = button;
    
                UINavigationBar.Appearance.StandardAppearance = style;
            }
            else
            {
                var attr = UIBarButtonItem.Appearance.GetTitleTextAttributes(UIControlState.Application);
                attr.Font = UIFont.SystemFontOfSize(15);
                attr.TextColor = UIColor.Red;
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Normal);
                UIBarButtonItem.Appearance.SetTitleTextAttributes(attr, UIControlState.Highlighted);
            }
    

    Thanks a lot! It works fine :blush:

  • benjffbenjff Member
    edited March 11

    Has anyone else managed to get this to work? I'm running the latest VS and Xamarin.iOS with an emulated iOS 13.7 and 14.4 and this solution does nothing. Should the code go somewhere other than AppDelegate.cs?

    I've literally reduced this to the most simple version I can think of (to set the nav bar title to yellow throughout the app) to test the code works and it's just doing nothing :neutral:

            var style = new UINavigationBarAppearance();
    
            var a = new UIStringAttributes()
            {
                ForegroundColor = UIColor.Yellow
            };
            style.TitleTextAttributes = a;
    
            UINavigationBar.Appearance.StandardAppearance = style;
    
  • JohnHJohnH GBMember ✭✭✭✭✭
    edited March 17

    @ColeX @Yelinzh there is a problem with the Microsoft Q&A site, @Benjff and I are trying to get an answer to this question by posting there but it is throwing access denied errors.

Sign In or Register to comment.