1
votes

I'm using iOS/Xamarin

When the user clicks a button, I'd like to change the background color of my view controller's main view (View).

In the button click event, I tried this:

partial void MyButton_TouchUpInside(UIButton sender)
{
    InvokeOnMainThread(() =>
    {
        SetBackgroundColor(View);
    });
}

but it doesn't work. I tried a few other main thread/uithread things but no luck.

If I call SetBackgroundColor(View) from the ViewDidLoad of course it works. I suspect this is a UI thread issue.

Any advice?

Thanks!

2

2 Answers

1
votes

You can use the BackgroundColor property on the UIViewController's View property.

partial void MyButton_TouchUpInside(UIButton sender)
{
    View.BackgroundColor = UIColor.Red;
}
0
votes

Turns out the above code is good - somewhere else in the code someone was setting another layer on top of the background so I never saw it =/