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

标签 ios image swift button cornerradius

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

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/

相关文章:

ios - 如何动态更改webView的高度?

c++ - 我使用 AudioKit 使用什么语言?

php - 使用从 phpmyadmin(MySQL) 派生的 PHP 将图像显示为图库

swift - 我应该怎么做才能在tableView中加载异步图像?

ios - 页面 View Controller 不显示点

objective-c - 通过 UITableView 从 NSMutableDictionary 添加/删除项目

ios - 无法在 OS X El Capitan 中使用命令 genstrings

ios - 在浏览器中启动应用后验证

java - 如何在JAVA中用swing缓慢(像效果)移动标签(图像)到鼠标点击位置

css - 不确定如何从 css 中的图像中删除下划线