如何在 xamarin 中为 UIImageView 设置静态高度和宽度 iOS

How to set a static height and width for a UIImageView in xamarin iOS

所以我正在为一个选择器开发一个 CustomRenderer,其中我需要在选择器的右侧放置一个图像,我能够做到这一点,但是图像尺寸太大,我该如何设置静态大小,

我试过这样做,但我不确定我是否在正确的轨道上

 Control.BorderStyle = UITextBorderStyle.None;
                var downarrow = UIImage.FromBundle(element.PickerImageSource);
                Control.RightViewMode = UITextFieldViewMode.Always;
                var imgView = new UIImageView(downarrow);
                imgView.Frame = new CoreGraphics.CGRect(0.1, 0.1, 0.1, 0.1);
                Control.RightView = imgView;

任何输入都会有所帮助

有一个ContentMode属性的UIImageView

Controls how the cached bitmap of a view must be rendered when the view's bounds change.

并且从ContentMode Enum,有一个ScaleAspectFit让图片适合UIImageView的frame

Scales the contents so that everything is visible, while preserving the aspect ration. Any areas that are not filled become transparent.

因此,您可以通过添加此 属性:

来修改代码
Control.BorderStyle = UITextBorderStyle.None;
var downarrow = UIImage.FromBundle(element.PickerImageSource);
Control.RightViewMode = UITextFieldViewMode.Always;
var imgView = new UIImageView(downarrow);
imgView.Frame = new CoreGraphics.CGRect(0.1, 0.1, 0.1, 0.1);

// modify here
imgView.ContentMode = UIViewContentMode.ScaleAspectFit;

Control.RightView = imgView;

UIViewContentMode.ScaleAspectFit 对我不起作用...但是,我想出了另一个解决方案:

var template = new UIView();
template.Frame = new CGRect(0, 0, 20, 20);

var image = new UIImageView();
image.Frame = new CGRect(0, 0, 20, 20);
image.Image = UIImage.FromBundle("ic_caret_down");
template.AddSubview(image);

Control.RightViewMode = UITextFieldViewMode.Always;
Control.RightView = template;