首页 > 解决方案 > 快速创建按钮

问题描述

我正在尝试快速实现以下按钮。

在此处输入图像描述

这是我到目前为止所拥有的

let startBtn = UIButton(frame: CGRect(x: (self.view.frame.width - 319)/2, y: progressView.frame.maxY + 10, width: 319, height: 48))
startBtn.backgroundColor = UIColor(red: 0.00, green: 0.83, blue: 0.69, alpha: 1.00);
startBtn.layer.cornerRadius = 40
startBtn.setTitleColor(UIColor.white, for: .normal)
startBtn.titleLabel?.textAlignment = .center
startBtn.contentVerticalAlignment = .center
startBtn.titleLabel?.font = UIFont(name: "CeraPro-Medium", size: 17);

if (self.courseProgress != nil && self.courseProgress >= 0) {
    startBtn.setTitle("Resume Course", for: .normal)
} else {
    if (hasPurchased) {
        startBtn.setTitle("Start Course", for: .normal)
    } else {
        if let price = self.data.price {
            startBtn.setTitle("Buy this course for $\(price)", for: .normal)
        }
    }
}
startBtn.addTarget(self, action:#selector(self.goToCourse(sender:)), for: .touchUpInside)
headerView.addSubview(startBtn)

不幸的是,上面的代码给了我以下 在此处输入图像描述

我怎样才能使该代码看起来像我展示的设计?

标签: iosswift

解决方案


圆角半径问题。

您可能应该通过以下方式设置角半径:

startBtn.layer.cornerRadius = startBtn.bounds.height / 2

它允许正确地圆角按钮


推荐阅读