UIView怎么设置成圆角

2025-05-08 01:08:09
推荐回答(2个)
回答1:

先添加QuartzCore.framework这个库,还有在你的文件中包含#import 。
在实际的应用中,我们总感觉圆角的东西比较好看, 像button,label,image等等,以前的时候我就经常给那些控件添加一个UIImageView作为背景,再搞张圆角的图片
viewT.layer.cornerRadius = 10;//设置那个圆角的有多圆
viewT.layer.borderWidth = 10;//设置边框的宽度,当然可以不要
viewT.layer.borderColor = [[UIColor redColor] CGColor];//设置边框的颜色
viewT.layer.masksToBounds = YES;//设为NO去试试。设置YES是保证添加的图片覆盖视图的效果
UIImageView *v=[[UIImageView alloc] initWithFrame:CGRectMake(25, 25, 100, 100)]; v.image=[UIImage imageNamed:@"face01.png"]; [[v layer] setBorderWidth:2.0];//画线的宽度 [[v layer] setBorderColor:[UIColor blackColor].CGColor];//颜色 [[v layer]setCornerRadius:15.0];//圆角 v.backgroundColor=[UIColor redColor]; //[v.layer setCornerRadius:8.0]; [v.layer setMasksToBounds:YES]; [self.view addSubview:v]; [v release]

回答2:

UIImageView *v=[[UIImageView alloc] initWithFrame:CGRectMake(25, 25, 100, 100)];
v.image=[UIImage imageNamed:@"face01.png"];
[[v layer] setBorderWidth:2.0];//画线的宽度
[[v layer] setBorderColor:[UIColor blackColor].CGColor];//颜色
[[v layer]setCornerRadius:15.0];//圆角
v.backgroundColor=[UIColor redColor];

//[v.layer setCornerRadius:8.0];
[v.layer setMasksToBounds:YES];
[self.view addSubview:v];

[v release];
还有一种更简便的方法
--------------------------------
其实,这件事情没有这么复杂的。
只需要加两句话就够了:
这里给个例子吧:
UIButton * takepicbtn = [UIButton buttonWithType:UIButtonTypeCustom];
takepicbtn.frame = CGRectMake(320-50, 250-50, 50, 50);
加下面两句话:
takepicbtn.layer.cornerRadius = 50/2;/////////////////////////自己试着改变这个值,看看各个不同的效果。现在的设置是画了个圆圈。
takepicbtn.layer.masksToBounds = YES;//////////////////////////