按钮图像不垂直居中

时间:2015-08-10 14:12:47

标签: c# ios xamarin xamarin.ios imagebutton

我已经将一个按钮子类化,这样我基本上可以得到一个带有圆边和右侧图标的按钮。

几乎是正确的,唯一的问题是图标没有垂直居中,似乎无法弄清楚我如何影响垂直对齐。

原始图像是矩形的,我正在使用CreateResizableImage和预定义的UIEdgeInsets结构,如下所示,但我所看到的是:

enter image description here

我的子类代码是:

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

2 个答案:

答案 0 :(得分:1)

我在xamarin forum

上发现了同样的问题

据了解,你应该把YAlign =" Center"在你的形象

答案 1 :(得分:1)

经过多次试验和错误后发现,这似乎适用于我的代码:

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

stretchingButtonImage.ImageWithAlignmentRectInsets (btnEdgeInsets);

似乎我不应该使用

  

CreateResizableImage