如何用swift添加圆形按钮?

时间:2015-12-15 09:16:19

标签: ios swift button interface-builder

我正在尝试制作这个简单的用户界面

现在橙色和深蓝色是简单的两个视图(深蓝色将具有嵌套的tableview),但是如何使用一些简单的动画制作按钮?我应该使用CALayer还是可以使用Interface Builder?

enter image description here

2 个答案:

答案 0 :(得分:5)

我会通过代码完成它:

    let button2 = UIButton()
    button2.frame = CGRectMake(0, 0, 100, 100)
    button2.layer.borderColor = UIColor.whiteColor().CGColor
    button2.layer.borderWidth = 2
    button2.layer.cornerRadius = 50
    button2.setTitle("button", forState: .Normal)
    button2.backgroundColor = UIColor.blueColor()
    button2.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
    button2.setTitleColor(UIColor(red: 233/255, green: 64/255, blue: 87/255, alpha: 1), forState: UIControlState.Normal)

self.view.addSubview(button1)

答案 1 :(得分:1)

试试这个: -  确保按钮的高度和宽度相同

self.imgBg1.layer.cornerRadius = self.imgBg1.bounds.size.height / 2
self.imgBg1.layer.borderWidth = 3.0
self.imgBg1.layer.borderColor = UIColor.whiteColor().CGColor
self.imgBg1.clipsToBounds = true
self.imgBg1.contentMode = .ScaleToFill