UIView或UIImageView如何实现圆角?

8

我需要给一个 UIImageView 添加圆角。我在论坛上找到了一个解决方案,然后我尝试了下面的代码:

UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

我使用的是iOS 5,但是找不到 setMasksToBounds:YESsetCornerRadius

那么在我的 UIImageview 中有没有其他方法可以实现圆角呢?


你尝试将 l 赋值回 roundedView 的图层属性了吗? - fearmint
怎么做?你能给我展示一下它会是什么样子吗? - Illep
我没有尝试过, roundedView.layer = l; - fearmint
3个回答

14
为了在一个UIView(或它的子类UIImageView)上制作圆角,您需要像您在问题中所写的那样,在图层上设置cornerRadius。例如:
theRoundedCornersView.layer.cornerRadius = 10.0f;

导入正确的头文件即可编译:

#import <QuartzCore/QuartzCore.h>

别忘了将它添加到你的框架中以链接它。


4
#import <QuartzCore/QuartzCore.h>

并链接到QuartzCore


1
将这行代码添加到你的 .h 文件中:
#import <QuartzCore/QuartzCore.h>

...... 警告将消失(不导入也可以正常工作)。您的问题与iOS 5无关。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接