iOS - UITableViewCell image adjust/resize

后端 未结 4 707
抹茶落季
抹茶落季 2020-12-29 09:08

I have create a table view and have filled its cells with text. What I\'m trying to do is add an image to the cell but whats happening is the image is coving the whole cell

4条回答
  •  情话喂你
    2020-12-29 09:42

    First you need to set the cell's imageView to the max size you want. Then assure via Interface Builder or via code that you have the correct content mode:

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    Basically with this mode it will try to fit the space you created for your image but it will maintain the aspect ratio.

    Jackson posted a good image that shows the content modes here:

    enter image description here

    For the corner you will need QuartzCore and you can do set it with this code:

    #import 
    
    ...
    
    cell.imageView.layer.cornerRadius = 4;
    cell.imageView.layer.masksToBounds = YES;
    

    UPDATE

    If your imageView isn't an outlet you are better off adding one imageView yourself to the cell. You can (and you should) do that via storyboard/xib. This tutorial might help you on that, although there are a lot of them on the internet. This stackoverflow question gives other solutions to that problem.

提交回复
热议问题