UIButtonConfiguration problem updating UIButton

Pelister 0 Reputation points
2023-03-06T17:57:56.4333333+00:00

I have a Xamarin.iOS app that uses the new UIButtonConfiguration API for styling buttons. I have a problem with updating the button fields in code, such as color and title. I'm using VS Mac 17.5, Xcode 14.2, Xamarin.iOS 16.2.

I've tried the below methods to update the button; neither of the titles and neither of the colors are applied. What could be the problem?

public partial class ViewController : UIViewController
{
    public ViewController(IntPtr handle) : base(handle) { }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        this.MyButton.ConfigurationUpdateHandler += this.UpdateHandler;
        this.MyButton.TouchUpInside += (s, a) =>
        {
            this.MyButton.Configuration.Title = "test 1";
            this.MyButton.Configuration.BaseForegroundColor = UIColor.Red;
            this.MyButton.SetNeedsUpdateConfiguration();
        };
    }

    private void UpdateHandler(UIButton button)
    {
        button.Configuration.Title = "test 2";
        button.Configuration.BaseForegroundColor = UIColor.Green;
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,286 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 25,521 Reputation points Microsoft Vendor
    2023-03-07T06:41:23.84+00:00

    Hello,

    ConfigurationUpdateHandler is to respond changes to the button’s state, the button's state is Normal, it doesn't change after you click the button (and you didn't set different config for different button state). Calling SetNeedsUpdateConfiguration method will make the system call updateConfiguration. The system calls this method automatically when the button’s state changes. Please see updateConfiguration | Apple Developer Documentation

    If you want to change the configuration when you click the button, you can refer to the following code:

    public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
    
              this.MyButton.Tag = 1;//set tag for testing
               this.MyButton.ConfigurationUpdateHandler += this.UpdateHandler;
    
               this.MyButton.TouchUpInside += (s, a) =>
                {
                    MyButton.Tag = MyButton.Tag ==1?0:1;// change tag when you click the button
                };
            }
            private void UpdateHandler(UIButton button)
            {
                var config = UIButtonConfiguration.PlainButtonConfiguration;
                config.Title = "test1";
                config.BaseForegroundColor = UIColor.Green;
               /* it's recommended to change the config according to the state*/
                //switch (button.State)
                //{
                //    case UIControlState.Normal:
                //        config.Title = "Normal"; //or"test1"
                //        config.BaseForegroundColor = UIColor.Purple;
                //        break;
                //    case UIControlState.Selected:
                //        config.Title = "Selected";
                //        config.BaseForegroundColor = UIColor.Yellow;
                //        break;
                //    case UIControlState.Disabled:
                //        config.Title = "Disabled";
                //        config.BaseForegroundColor = UIColor.LightGray;
                //        break;
                //...and other states
                //    default:
                //        config.Title = "test1";
                //        config.BaseForegroundColor = UIColor.Green;
                //        break;
                //}
    
               if (button.Tag == 1)
                {
                    config.Title = "test2";
                    config.BaseForegroundColor = UIColor.Red;
                }
                button.Configuration = config;
    
           }
    
    

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.