简体   繁体   English

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

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

I'm trying to create an image with rounded corners and ass 5pts of padding around the image. 我正在尝试创建一个带有圆角的图像,并在图像周围填充约5pt的填充。 I've got the corners working, but the image, an icon, takes up the entire control. 我已经做好了工作,但是图像(一个图标)占据了整个控件。 Some things that I've tried are: 我尝试过的一些事情是:

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

In Android I was able to do this using 在Android中,我可以使用

            child.SetPadding(20,20,20,20);
            var result = base.DrawChild(canvas, child, drawingTime);

How do I do this in iOS? 如何在iOS中执行此操作?

It looks like in iOS I should be able to do this by setting the Frame on the control like this: 看起来在iOS中,我应该能够通过如下设置控件上的Frame来做到这一点:

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

taken from https://stackoverflow.com/a/6435536/1181230 . 取自https://stackoverflow.com/a/6435536/1181230 But the image size is now adjusted to the frame. 但是图像大小现在已根据帧进行了调整。 Is this a bug in Forms? 这是Forms中的错误吗?

I ended up overriding TrySetImage and called my own SetImage, which creates a new imageview with a frame .. I replaced: 我最终重写了TrySetImage并调用了我自己的SetImage,它使用框架创建了一个新的imageview。

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

with: 与:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM