Swift - 设置圆角样式的图片,圆角样式的按钮
1,圆角按钮
通过UIButton的layer的cornerRadius属性设置,可以很方便的实现圆角按钮。 下面是带边框和不带边框两种按钮的效果图
通过UIButton的layer的cornerRadius属性设置,可以很方便的实现圆角按钮。 下面是带边框和不带边框两种按钮的效果图

/*** 不带边框的按钮 ***/ let button2 = UIButton(frame: CGRectMake(20, 120, 200, 45)) button2.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:1) //设置圆角 button2.layer.cornerRadius = 10 button2.setTitle("hangge.com", forState: UIControlState.Normal)//设置按钮标题 button2.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)//设置按钮标题颜色 self.view.addSubview(button2) /*** 带边框的按钮 ***/ let button1 = UIButton(frame: CGRectMake(20, 50, 200, 45)) button1.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:1) //设置圆角 button1.layer.borderColor = UIColor.whiteColor().CGColor button1.layer.borderWidth = 2 button1.layer.cornerRadius = 10 button1.setTitle("hangge.com", forState: UIControlState.Normal)//设置按钮标题 button1.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)//设置按钮标题颜色 self.view.addSubview(button1)
2,圆角图片
对于其他的UI组件,同样也是可以通过设置cornerRadius来实现圆角效果

//圆角图片 let image1 = UIImageView(frame: CGRectMake(20,200,240,180)) image1.image = UIImage(named: "IMG_0297.jpg") image1.layer.masksToBounds = true; image1.layer.cornerRadius = 10 self.view.addSubview(image1)