ios - 我怎样才能用 Swift 编程渐变边框颜色 UIButton

标签 ios iphone uibutton swift2 gradient

<分区>

如何以编程方式设计像这种渐变边框颜色的 UIButton?

Gradient button ]

感谢帮助

最佳答案

let gradient = CAGradientLayer()
gradient.frame =  CGRect(origin: CGPointZero, size: self.myButton.frame.size)
gradient.colors = [UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]

let shape = CAShapeLayer()
shape.lineWidth = 2
shape.path = UIBezierPath(rect: self.myButton.bounds).CGPath
shape.strokeColor = UIColor.blackColor().CGColor
shape.fillColor = UIColor.clearColor().CGColor
gradient.mask = shape

self.myButton.layer.addSublayer(gradient)

Swift 3 版本:

    let gradient = CAGradientLayer()
    gradient.frame =  CGRect(origin: CGPoint.zero, size: self.myButton.frame.size)
    gradient.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]

    let shape = CAShapeLayer()
    shape.lineWidth = 2
    shape.path = UIBezierPath(rect: self.myButton.bounds).cgPath
    shape.strokeColor = UIColor.black.cgColor
    shape.fillColor = UIColor.clear.cgColor
    gradient.mask = shape

    self.myButton.layer.addSublayer(gradient)

关于ios - 我怎样才能用 Swift 编程渐变边框颜色 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36836367/

相关文章:

iphone - 如何忽略正则表达式中的空格

ios - Google+ 邀请 - iOS SDK/API - 是否有类似于 Facebook 邀请的实用方法来邀请 Google Plus 用户

ios - 消除uitableview tablefooterview分隔符

ios - 'NSBatchDeleteRequest' 仅适用于 iOS 9.0 或更新版本

iphone - 我的iPhone应用程序查看页面上的“报告问题”链接?

ios - 自定义 UIButton 不起作用

ios - [SLComposeViewController initWithExtension :requestedServiceType:]_block_invoke + 84 崩溃

iphone - 在搜索方法迭代完成后重新加载/显示 UISearchDisplayController 的 searchResultsTableView

ios - 是否可以在类里面从 IB 获得按钮标题?

ios - 如何通过编码xcode改变按钮的位置