Setting Corner Radius on UIImageView not working

前端 未结 11 1872
心在旅途
心在旅途 2020-12-22 16:15

I\'m at a bit of a loss. I\'ve used the layer property of UIView to round the corners of multiple elements in my app. However, this one UIImageView is simply not complying.

相关标签:
11条回答
  • 2020-12-22 16:51

    Swift 4.2 Answer:

    In order for the corner radius to work, add the image to a UIView and then you need to set the image's masksToBounds property to true:

    planeImage.layer.masksToBounds = true
    planeImage.layer.cornerRadius = 20
    

    Note: Replace 20 by the desired cornerRadius

    0 讨论(0)
  • 2020-12-22 16:56
    -(void) viewDidAppear:(BOOL)animated{
           [super viewDidAppear:animated];
           [self setMaskTo:viewDistance 
                 byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
               }
    
    - (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
         {
          UIBezierPath *rounded = [UIBezierPath 
                    bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
    
                         cornerRadii:CGSizeMake(20.0, 20.0)];
    
              CAShapeLayer *shape = [[CAShapeLayer alloc] init];
              shape.frame = self.view.bounds;
             [shape setPath:rounded.CGPath];
              view.layer.mask = shape;
           }
    
    0 讨论(0)
  • 2020-12-22 16:57

    Also worth noting that

    1. If you are using aspectFit AND cornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.

    i.e if you have this

    theImageView.contentMode = .scaleAspectFit
    

    and

       theImageView.layer.cornerRadius = (theImageView.frame.size.height)/2
        theImageView.clipsToBounds = true
    

    or

    theImageView.layer.masksToBounds = true
    

    It won't work. you will have to get rid of aspectFit code

    //theImageView.contentMode = .scaleAspectFit
    
    1. Make sure the width and the height for the Image View is same
    0 讨论(0)
  • 2020-12-22 16:58

    This should work

    cell.previewImage.clipsToBounds = YES;
    
    cell.previewImage.layer.cornerRadius = 20;
    
    0 讨论(0)
  • 2020-12-22 17:02

    Nothing from the previous answers is working?

    It may happen that the size of the UIImageView is bigger than the image size. Corner radius can be set well but not visible in that case.

    Quick check of the UIImageView size by code: (or can use "View UI Hierarchy" tool in XCode)

    self.imageView.backgroundColor = [UIColor greenColor]; 
    

    In this scenario you should assure that UIImageView has the same aspect ratio as the image.

    0 讨论(0)
  • 2020-12-22 17:03

    In Xcode Interface Builder, selecting 'Clip Subviews' Drawing attribute for the view together with setting the corner radius in the code cell.previewImage.layer.cornerRadius = 20;does the job for me!

    See 'Clip Subviews' option in IB

    0 讨论(0)
提交回复
热议问题