programing tip

UIImageView에서 코너 반경 설정이 작동하지 않습니다

itbloger 2020. 7. 13. 21:38
반응형

UIImageView에서 코너 반경 설정이 작동하지 않습니다


나는 약간의 손실에 있습니다. UIView의 레이어 속성을 사용하여 앱에서 여러 요소의 모서리를 둥글게했습니다. 그러나이 UIImageView는 단순히 준수하지 않습니다. 내가 무엇을 놓치고 있는지 잘 모르겠습니다.

UIImageView (previewImage라고 함)는 테이블 뷰 셀에 포함되어 있습니다. cornerRadius 속성을 여러 위치 (셀 자체와 셀을 만드는 컨트롤러)에서 사용할 수 없도록 설정하려고했습니다.

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.
}

누락 된 셀이로드되는 방식에 대한 것이 있습니까?


레이어의 masksToBounds속성을 YES다음과 같이 설정해야 합니다 .

cell.previewImage.layer.masksToBounds = YES;

이는 UIImageView컨트롤이 UIImage객체 를 보유 할 의사 서브 뷰를 생성 하기 때문 입니다.


또한 주목할 가치가 있습니다.

  1. clipsToBounds / masksToBounds와 함께 aspectFit AND cornerRadius를 사용 하는 경우 둥근 모서리를 얻지 못합니다.

즉 당신이 이것을 가지고 있다면

theImageView.contentMode = .scaleAspectFit

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

또는

theImageView.layer.masksToBounds = true

그것은 작동하지 않습니다 . aspectFit 코드를 제거해야합니다.

//theImageView.contentMode = .scaleAspectFit
  1. 이미지보기너비와 높이가 동일한 지 확인하십시오

이 작동합니다

cell.previewImage.clipsToBounds = YES;

cell.previewImage.layer.cornerRadius = 20;

나는 당신이 설정해야한다고 생각합니다 :

cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;

Xcode Interface Builder에서 뷰의 'Clip Subviews'Drawing 속성을 선택하고 코드에서 모서리 반경을 설정하면 cell.previewImage.layer.cornerRadius = 20;나에게 도움이됩니다!

IB의 'Clip Subviews'옵션 참조


아래 코드에서 시도하십시오.

cell.previewImage.layer.cornerRadius = 20;
cell.previewImage.clipsToBounds = YES;

이 코드를 사용해보십시오 :-

self.imgaviewName.clipsToBounds = true
self.imageviewName.layer.cornerRadius = 10

-(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;
       }

스위프트 4.2 답변 :

모서리 반경이 작동하려면에 이미지를 추가 UIView한 다음 이미지 masksToBounds속성을 다음과 같이 설정해야 합니다 true.

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

Note: Replace 20 by the desired cornerRadius

참고URL : https://stackoverflow.com/questions/4314640/setting-corner-radius-on-uiimageview-not-working

반응형