1

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 enter image description here

Venky
  • 1,929
  • 3
  • 21
  • 36

2 Answers2

1

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;
Junior Jiang
  • 12,430
  • 1
  • 10
  • 30
1

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;
  • This is the correct answer for iOS 13+. Starting in iOS 13, the implementation of leftViewRect(forBounds:) and rightViewRect(forBounds:) now ask the view for its systemLayoutSizeFitting(:). To achieve the previous behavior when linking against and running on iOS 13, add explicit sizing constraints on the view, wrap it in a plain UIView. – Arvis Mar 16 '22 at 00:07