How to manage UIImageView content mode?

后端 未结 7 2092
萌比男神i
萌比男神i 2020-11-29 03:16

I have an UIImageView with some fixed rect size. But my images are not fixed in size. I want to display images according to UIImageView\'s rect size. Whether image is big or

相关标签:
7条回答
  • 2020-11-29 03:45

    Worth noting that swift 3 has changed as following

    .ScaleAspectFit to scaleAspectFit

    .ScaleAspectFill to .scaleAspectFill

    0 讨论(0)
  • 2020-11-29 03:46

    Here a solution for swift:

    let imageView = UIImageView(image: UIImage(named: "backgroundImage"))
    imageView.frame = tableView.frame
    imageView.contentMode = .ScaleAspectFill
    tableView.backgroundView = imageView
    
    0 讨论(0)
  • 2020-11-29 03:49

    If you want to make your UIImageView size according to ratio ,then you must set it's mode to UIViewContentModeScaleAspectFit

     yourimage.contentMode=UIViewContentModeScaleAspectFit;
    

    let me know it is working or not!!!

    Happy Coding.

    0 讨论(0)
  • 2020-11-29 03:52

    In swift you set content mode as well

    var newImgThumb : UIImageView
    newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
    newImgThumb.contentMode = .ScaleAspectFit
    
    0 讨论(0)
  • 2020-11-29 04:02

    Please try this code, Hope it will work for you.

    set UIImageView contentMode to UIViewContentModeScaleAspectFill as below :

    imageView.contentMode = UIViewContentModeScaleAspectFill;
    

    set autoresizingMask of UIImageView as below :

    imageView.autoresizingMask =   
        ( UIViewAutoresizingFlexibleBottomMargin
        | UIViewAutoresizingFlexibleHeight
        | UIViewAutoresizingFlexibleLeftMargin
        | UIViewAutoresizingFlexibleRightMargin
        | UIViewAutoresizingFlexibleTopMargin
        | UIViewAutoresizingFlexibleWidth );
    
    0 讨论(0)
  • 2020-11-29 04:05

    You just need these two lines:

    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow
    
    0 讨论(0)
提交回复
热议问题