gpt4 book ai didi

ios - 如何在 Button 内将 cornerRadius 设置为 UIImage?

转载 作者:行者123 更新时间:2023-11-28 09:46:21 25 4
gpt4 key购买 nike

我正在创建带有渐变的图像,我希望按钮具有角半径

button = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.frame = CGRectMake(self.view.frame.size.width/2 - button.frame.size.width, 100, 250, 50)
button.layer.cornerRadius = 3
button.setTitle("ViewThree", forState: UIControlState.Normal)
button.addTarget(self, action: "ViewControllerAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.setBackgroundImage(getImageWithGradient(UIColor(netHex:0x2d72cf).CGColor, bottom: UIColor(netHex:0x2d72cf).CGColor, size: CGSize(width: button.bounds.width, height: button.bounds.height), frame: button.bounds), forState: UIControlState.Normal)

func getImageWithGradient(top: CGColor, bottom: CGColor, size: CGSize, frame: CGRect) -> UIImage{
var image = UIImage()
UIGraphicsBeginImageContext(frame.size)
var context = UIGraphicsGetCurrentContext()

image.drawAtPoint(CGPointMake(0, 0))

let colorSpace = CGColorSpaceCreateDeviceRGB()
let locations:[CGFloat] = [0.0, 1.0]


let gradient = CGGradientCreateWithColors(colorSpace,
[top, bottom], locations)

let startPoint = CGPointMake(frame.size.width / 2, 0)
let endPoint = CGPointMake(frame.size.width / 2, frame.size.height)
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0)

image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return image

}

但是无论我怎么尝试都没有圆角半径,我该如何尝试

最佳答案

尝试将 UIButtonclipToBounds 属性设置为 YES

关于ios - 如何在 Button 内将 cornerRadius 设置为 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30580915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com