iosxamarin.formsxamarin.iosuiimageviewcustom-renderer

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


Solution

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