Setting Corner Radius on UIImageView not working

2019-01-29 19:21发布

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. Not sure what I am missing.

The UIImageView (called previewImage) is contained in a Table View Cell. I've tried setting the cornerRadius property multiple location (in the cell itself and in the controller that creates the cell) to no avail.

static NSString *CellIdentifier = @"MyTableViewCell";

MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
    cell.previewImage.layer.cornerRadius = 20; //Made it 20 to make sure it's obvious.
}

Is there something about the way cells are loaded that I'm missing?

8条回答
何必那么认真
2楼-- · 2019-01-29 19:48

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
查看更多
放荡不羁爱自由
3楼-- · 2019-01-29 19:50

I believe you need to set:

cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;
查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-29 19:56

Try this code:-

self.imgaviewName.clipsToBounds = true
self.imageviewName.layer.cornerRadius = 10
查看更多
▲ chillily
5楼-- · 2019-01-29 19:59

This should work

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;
查看更多
地球回转人心会变
6楼-- · 2019-01-29 20:00
-(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;
       }
查看更多
冷血范
7楼-- · 2019-01-29 20:06

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

查看更多
登录 后发表回答