【问题标题】:Creating a button in swift快速创建按钮
【发布时间】:2020-10-17 21:17:36
【问题描述】:

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

这是我目前所拥有的

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)

不幸的是,上面的代码给了我以下信息

如何使代码看起来像我展示的设计?

【问题讨论】:

标签: ios swift


【解决方案1】:

圆角半径问题。

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

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

它允许正确地圆角按钮

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2021-07-15
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多