带有填充和圆角的图标 - iOS Xamarin Forms 的自定义渲染器

Icon with padding and rounded corners - iOS Custom Renderer for Xamarin Forms

我正在尝试创建一个圆角图像,并在图像周围留出 5 磅的边距。我有角落工作,但图像,一个图标,占据了整个控制。我尝试过的一些事情是:

    public override void LayoutSubviews()
    {
        try
        {
            var element = (IconTile)Element;
            var textheight = element.HeightRequest / 3;

            Control.Layer.CornerRadius = 10;
            Control.Layer.BackgroundColor = HelperUtilities.GetColor(element.Item.BackgroundColor, Color.Transparent).ToCGColor();
            Control.ClipsToBounds = true;

            //Control.Image.Scale(new CGSize(element.WidthRequest - 10, element.HeightRequest - 10));
            Control.Image = Control.Image.ImageWithAlignmentRectInsets(new UIEdgeInsets(5, 5, 5, 5));
            Control.Frame = new CGRect(5, 5, element.Width - 10, element.Height - 10);
            Control.LayoutMargins = new UIEdgeInsets(5,5,5,5);
            Control.Image.DrawAsPatternInRect(new CGRect(5, 5, element.Width - 10, element.Height - 10));
            //UIGraphics.BeginImageContextWithOptions(new CGSize(element.WidthRequest - 40, element.HeightRequest - 40), false, 0f);
            base.LayoutSubviews();

在 Android 中,我能够使用

做到这一点
            child.SetPadding(20,20,20,20);
            var result = base.DrawChild(canvas, child, drawingTime);

如何在 iOS 中执行此操作?

看起来 iOS 我应该可以通过像这样在控件上设置框架来做到这一点:

            CGSize size = Control.Image.Size;
            CGRect rect = new CGRect(0, 0, size.Width - 5, size.Height - 5);
            Control.Frame = rect;

取自 。但是图像大小现在已根据框架进行了调整。是 这是表单中的错误?

我最终覆盖了 TrySetImage 并调用了我自己的 SetImage,它创建了一个带有框架的新 imageview .. 我替换了:

            var imageView = Control;
            if (imageView != null)
                imageView.Image = uiimage;

与:

            var imageOnly = ((IconTile) Element).ImageOnly;
            _imageView = new UIImageView(imageOnly ? new CGRect(7, 7, Element.WidthRequest - 14, Element.HeightRequest - 14)
                : new CGRect(10, 10, Element.WidthRequest - 20, Element.HeightRequest - 20))
            {
                Image = uiimage,
                ClipsToBounds = true,
            };
            Control.InsertSubview(_imageView, 0);