Button image not centering vertically

Hi

I have subclassed a button such that I can get basically a button with rounded edges and an icon on the right side.

Nearly got it right, only issue is that the icon is not centered vertically and cannot seem to figure out how I can influence the vertical alignment.

The original image is rectangular and I am using CreateResizableImage with a pre-defined UIEdgeInsets struct as below but what I am seeing is:

My sub class code is as follows:

partial class CustomGreyButton : UIButton
    {
        public CustomGreyButton (IntPtr handle) : base (handle)
        {
            this.Layer.BorderColor = new CoreGraphics.CGColor (0, 0, 0);
            this.Layer.BorderWidth = 1;
            this.Layer.CornerRadius = 20;

            // top, left, bottom, right
            UIEdgeInsets btnEdgeInsets = new UIEdgeInsets (0,44,0,44);

            UIImage stretchingButtonImage = new UIImage ("ios_images/grey_btn.png").CreateResizableImage (btnEdgeInsets);


            this.SetBackgroundImage (stretchingButtonImage, UIControlState.Normal);
            this.SetBackgroundImage (stretchingButtonImage, UIControlState.Selected);
            this.SetBackgroundImage (stretchingButtonImage, UIControlState.Highlighted);
            this.ClipsToBounds = true;
            this.AdjustsImageWhenHighlighted = false;

            //this.TitleColor = UIColor.White;
            this.SetTitleColor (UIColor.White, UIControlState.Normal);
            this.SetTitleColor (UIColor.Black, UIControlState.Highlighted);
            this.SetTitleColor (UIColor.Black, UIControlState.Selected);
        }
    }

Posts

  • JonShanksJonShanks GBMember

    After much trial and error found out that this seems to work for my code:

    UIImage stretchingButtonImage = new UIImage ("ios_images/grey_btn.png");
    
    stretchingButtonImage.ImageWithAlignmentRectInsets (btnEdgeInsets);
    

    Seems I shouldn't have been using

    CreateResizableImage

Sign In or Register to comment.