在 UIImageView 上设置圆角半径不起作用
我有点不知所措。我使用 UIView 的 Layer 属性来圆化应用程序中多个元素的角。然而,这个 UIImageView 根本不符合要求。不知道我错过了什么。
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.
}
我缺少有关单元格加载方式的信息吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
另外值得注意的是,
即如果你有这个
或者
它
不会工作。您必须删除aspectFit代码
Also worth noting that
i.e if you have this
and
or
It won't work. you will have to get rid of aspectFit code
这应该有效
This should work
我相信你需要设置:
I believe you need to set:
在 Xcode Interface Builder 中,为视图选择“Clip Subviews”绘图属性并在代码
cell.previewImage.layer.cornerRadius = 20;
中设置圆角半径即可为我完成这项工作!查看 IB 中的“剪辑子视图”选项
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
UIImageView 为cornerRadius 提供 ClipsToBounds 以下是 Swift 和 Objectiv-C 的示例(以下是圆形图像的示例代码)
Swift
Objectiv -C
UIImageView gives cornerRadius with clipsToBounds folowing is example of Swift and Objectiv-C (The followfing example code for round image)
Swift
Objectiv -C
试试下面的代码
Try this below piece of code
试试这个代码:-
Try this code:-
对于
imageView.contentMode = .scaleAspectFill
,如果图像非常大,则不会应用cornerRadius
,具体取决于硬件。调整全景图像大小的一些测试:
For
imageView.contentMode = .scaleAspectFill
thecornerRadius
is not applied if the image is very large, depending on the hardware.Some tests with resized panorama images:
The imageView dimensions where 160x100.
Interestingly, the newer hardware isn't necessarily more capable.
Feel free to edit this post with more test results!
Swift 4.2 答案:
为了使圆角半径起作用,请将图像添加到
UIView
中,然后需要将图像的masksToBounds
属性设置为true:
注意:将 20 替换为所需的
cornerRadius
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'smasksToBounds
property totrue
:Note: Replace 20 by the desired
cornerRadius
以前的答案没有任何作用?
UIImageView 的尺寸可能大于图像尺寸。圆角半径可以设置好,但在这种情况下不可见。
通过代码快速检查 UIImageView 的大小:(或者可以使用 XCode 中的“View UI Hierarchy”工具)
在这种情况下,您应该确保 UIImageView 与图像具有相同的宽高比。
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)
In this scenario you should assure that UIImageView has the same aspect ratio as the image.
您可以使用此代码
You can use this code
您需要将图层的
masksToBounds
属性设置为YES
:cell.previewImage.layer.masksToBounds = YES;
这是因为
UIImageView
控件创建一个伪子视图来保存UIImage
对象。You need to set the layer's
masksToBounds
property toYES
:cell.previewImage.layer.masksToBounds = YES;
This is because the
UIImageView
control creates a pseudo-subview to hold theUIImage
object.