【问题标题】:Button image not centering vertically按钮图像未垂直居中
【发布时间】:2015-08-10 14:12:46
【问题描述】:

我已经对一个按钮进行了子类化,这样我基本上可以获得一个带有圆形边缘和右侧图标的按钮。

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

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

我的子类代码是:

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

【问题讨论】:

  • 您可以上传您要添加的图形吗?您的 UIEdgeInsets 看起来不正确,但如果不看图形就很难分辨

标签: c# ios xamarin xamarin.ios imagebutton


【解决方案1】:

我在xamarin forum发现了同样的问题

作为理解,你应该把 YAlign="Center" 放在你的图片上

【讨论】:

  • 抱歉,我应该规定不使用 Xamarin.Forms。
【解决方案2】:

经过反复试验,发现这似乎适用于我的代码:

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

stretchingButtonImage.ImageWithAlignmentRectInsets (btnEdgeInsets);

看来我不应该使用

创建可调整大小的图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 2021-10-06
    • 1970-01-01
    • 2014-08-08
    • 2010-12-30
    • 1970-01-01
    相关资源
    最近更新 更多