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
객체 를 보유 할 의사 서브 뷰를 생성 하기 때문 입니다.
또한 주목할 가치가 있습니다.
- 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
- 이미지보기 의 너비와 높이가 동일한 지 확인하십시오
이 작동합니다
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;
나에게 도움이됩니다!
아래 코드에서 시도하십시오.
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
'programing tip' 카테고리의 다른 글
Rails 콘솔에서 멋진 포맷을 얻는 방법 (0) | 2020.07.13 |
---|---|
PHP 헤더가있는 CORS (0) | 2020.07.13 |
groovy 배열 / 해시 / 컬렉션 / 목록의 요소를 확인하는 방법은 무엇입니까? (0) | 2020.07.13 |
diff를 git-diff처럼 작동시키는 방법? (0) | 2020.07.12 |
angularjs 약속을 반환하기 전에 해결할 수 있습니까? (0) | 2020.07.12 |