Endjin - Home

Button Visual States in Xamarin.iOS

by Pascal Arnould

One way to manage a button visual state in iOS is by setting a different background image for each one of the UIControlState values available: Normal, Disabled, Highlighted and Selected. If all you want is a solid colour background for each specific state, you still have to create separate images, save them as bundle resources and refer back to them in code. This feels like a lot of effort for something which should be a bit more straight forward. However one of the answers to this question on Stack Overflow provided a better solution by creating a 1×1 pixel image programmatically based on a given color. Here’s the direct port of the code in C#:

Could not embed GitHub Gist c6444e76c2593cc6b0f3: Not Found

You can then use this utility method to create a button background images and set its visual states as shown below:

Could not embed GitHub Gist 8b3b281244f31c0057ca: Not Found

Pascal Arnould on Twitter

About the author