0

I'm trying to implement the following button in swift.

enter image description here

Here's what I have so far

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)

Unfortunately, the code above gives me the following enter image description here

How can I turn that code to look like the design I showed?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165
  • Look at the answer here, this might help: https://stackoverflow.com/questions/38874517/how-to-make-a-simple-rounded-button-in-storyboard/38874621#38874621 – realtimez Oct 17 '20 at 22:47
  • Please update your cornerRadius to 24 of buttons's layer because button's height is 48 – Cruz Oct 18 '20 at 02:22

1 Answers1

3

Problem with corner radius.

You should probably set corner radius the following way:

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

It allows to correctly round corners of the button

NikR
  • 628
  • 4
  • 14