ios 在 UIImageView 上设置角半径不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4314640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Corner Radius on UIImageView not working
提问by MarkPowell
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.
我有点不知所措。我已经使用 UIView 的 layer 属性来圆化应用程序中多个元素的角。但是,这个 UIImageView 根本不符合要求。不知道我错过了什么。
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.
UIImageView(称为 previewImage)包含在 Table View Cell 中。我已经尝试将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.
}
Is there something about the way cells are loaded that I'm missing?
是否有关于我缺少的单元格加载方式的信息?
回答by Evan Mulawski
You need to set the layer's masksToBounds
property to YES
:
您需要将图层的masksToBounds
属性设置为YES
:
cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.masksToBounds = YES;
This is because the UIImageView
control creates a pseudo-subview to hold the UIImage
object.
这是因为UIImageView
控件创建了一个伪子视图来保存UIImage
对象。
回答by Naishta
Also worth noting that
另外值得注意的是
- If you are using aspectFit ANDcornerRadius with clipsToBounds/masksToBounds, you won't get the rounded corners.
- 如果您使用带有 clipsToBounds/masksToBounds 的aspectFit ANDcornerRadius,您将不会得到圆角。
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
它不会工作。你将不得不摆脱 aspectFit 代码
//theImageView.contentMode = .scaleAspectFit
- Make sure the width and the height for the Image View is same
- 确保图像视图的宽度和高度相同
回答by Teena nath Paul
This should work
这应该工作
cell.previewImage.clipsToBounds = YES;
cell.previewImage.layer.cornerRadius = 20;
回答by Daniel Dickison
I believe you need to set:
我相信你需要设置:
cell.previewImage.layer.masksToBounds = YES;
cell.previewImage.layer.opaque = NO;
回答by ktheitroada
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!
在 Xcode Interface Builder 中,为视图选择“Clip Subviews”绘图属性并在代码cell.previewImage.layer.cornerRadius = 20;
中设置角半径对我来说很重要!
回答by Vaibhav Thakre
Try this below piece of code
试试下面这段代码
cell.previewImage.layer.cornerRadius = 20;
cell.previewImage.clipsToBounds = YES;
回答by kangna sharma
Try this code:-
试试这个代码:-
self.imgaviewName.clipsToBounds = true
self.imageviewName.layer.cornerRadius = 10
回答by IronmanX46
Swift 4.2 Answer:
斯威夫特 4.2 答案:
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
:
为了使圆角半径起作用,请将图像添加到 aUIView
然后您需要将图像的masksToBounds
属性设置为true
:
planeImage.layer.masksToBounds = true
planeImage.layer.cornerRadius = 20
Note: Replace 20 by the desired cornerRadius
注意:将 20 替换为所需的 cornerRadius
回答by Marek Manduch
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.
可能会发生 UIImageView 的大小大于图像大小的情况。拐角半径可以设置得很好,但在这种情况下不可见。
Quick check of the UIImageView size by code: (or can use "View UI Hierarchy" tool in XCode)
通过代码快速检查 UIImageView 大小:(或可以使用 XCode 中的“查看 UI 层次结构”工具)
self.imageView.backgroundColor = [UIColor greenColor];
In this scenario you should assure that UIImageView has the same aspect ratio as the image.
在这种情况下,您应该确保 UIImageView 具有与图像相同的纵横比。
回答by Maulik Patel
-(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;
}