0

How to change UIButton HighLight Color using Monotouch?

Jacob Foshee
  • 2,704
  • 2
  • 29
  • 48
Noro
  • 1,643
  • 5
  • 22
  • 38

3 Answers3

3

There is no direct way to do this. You must subclass UIButton and draw it your self when is is highlighted. This is a simple example:

public class CustomButton : UIButton
{

    public CustomButton(RectangleF frame)
    {

        this.Frame = frame;
        this.AddObserver(this, new NSString("Highlighted"), NSKeyValueObservingOptions.New, IntPtr.Zero);

    }

    public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
    {
        if (keyPath.ToString() == "Highlighted")
        {
            this.SetNeedsDisplay();
        }
    }

    public override void Draw (RectangleF rect)
    {
        base.Draw (rect);

        if (this.Highlighted)
        {
            // Draw for highlighted

        } else
        {

            // Draw for normal

        }
    }

}

I have translated this from this question, which has an Objective-C example: Here

Community
  • 1
  • 1
Dimitris Tavlikos
  • 8,170
  • 1
  • 27
  • 31
1

One possible way to do this, you will need to create a highlighted image for the button then use:

btn.SetImage(UIImage.FromBundle("Images/btnHighlighted.png"), UIControlState.Highlighted);
Luke
  • 3,665
  • 1
  • 19
  • 39
0

I have done this in my previos anwser to post so check link that i already implemented UIButton HighLigh Color

Community
  • 1
  • 1
IMMORTAL
  • 2,707
  • 3
  • 21
  • 37