简体   繁体   中英

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

So i'm working on a CustomRenderer for a Picker in which i need an image to the right side of the picker, i was able to do that, but the image size is too big, how can i set a static size to it,

i tried doing this, but i'm not sure if i'm on the right track

 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;

any inputs would be helpful在此处输入图片说明

There is a ContentMode property of UIImageView

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

And from the ContentMode Enum , there is a ScaleAspectFit to make the image fits the frame of UIImageView.

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

Therefore, you can modify code by adding this property:

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 did not work for me... However, I came up with another solution:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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