开发者

How to change UIButton HighLight Color using Monotouch?

开发者 https://www.devze.com 2023-03-03 16:25 出处:网络
How to change UI开发者_开发技巧Button HighLight Color using Monotouch?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 examp

How to change UI开发者_开发技巧Button HighLight Color using Monotouch?


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


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消