快速按钮的轮廓

时间:2017-04-22 16:45:37

标签: ios swift

我是快速编程的新手,我正在尝试制作钢琴应用程序。有谁知道在Swift的按钮上放置边框? 我在互联网上搜索,但是所有教程都是针对Swift的旧版本,而且它不再适用了。

3 个答案:

答案 0 :(得分:2)

UIButton继承自UIControl,UIControl继承自 UIView的。 UIView包含用于渲染的CALayer(核心动画层)。 https://developer.apple.com/reference/quartzcore/calayer

import UIKit
import PlaygroundSupport

let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.backgroundColor = .red
button.layer.borderWidth = 2.0
button.layer.borderColor = UIColor.green.cgColor

PlaygroundPage.current.liveView = button

答案 1 :(得分:0)

试试这个

            button.layer.borderWidth = 1.0
            button.layer.borderColor = UIColor.whiteColor().CGColor
            button.layer.cornerRadius = 5.0
            button.clipsToBounds = true

您可以轻松更改Button属性值,如(borderWidth,borderColor,cornerRadius)

答案 2 :(得分:-1)

以下是swift 3的代码

button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.white.cgColor
// if you want corners to be rounded you can use the corner radus
button.layer.cornerRadius = 4.0
// if you're setting background image to the button and it happens to be not clipped then you can use 
button.clipsToBounds = true