Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a circular button in Swift? [closed]

I want to make a circular thumbs up and thumbs down button.

Should I use a ImageView or a Button as the super class?

How would I do this in Swift?

like image 207
User Avatar asked Sep 26 '14 01:09

User


People also ask

How do I make a round button in Swift 4?

Set an aspect ratio constraint (width == height) so that your button is square. Then move the code button. layer. cornerRadius = ... to an override of viewDidLayoutSubviews .

How do I change the background color of a button in Swift?

Add a button on your storyboard, select it Go to it's attribute inspector and select 'Background' property to choose the color.


1 Answers

Here is an example round button:

Swift 3, 4, 5:

override func viewDidLoad() {     super.viewDidLoad()          let button = UIButton(type: .custom)     button.frame = CGRect(x: 160, y: 100, width: 50, height: 50)     button.layer.cornerRadius = 0.5 * button.bounds.size.width     button.clipsToBounds = true     button.setImage(UIImage(named:"thumbsUp.png"), for: .normal)     button.addTarget(self, action: #selector(thumbsUpButtonPressed), for: .touchUpInside)     view.addSubview(button) }  @objc func thumbsUpButtonPressed() {     print("thumbs up button pressed") } 

Swift 2.x:

override func viewDidLoad() {     super.viewDidLoad()          let button = UIButton(type: .Custom)     button.frame = CGRect(x: 160, y: 100, width: 50, height: 50)     button.layer.cornerRadius = 0.5 * button.bounds.size.width     button.clipsToBounds = true     button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)     button.addTarget(self, action: #selector(thumbsUpButtonPressed), forControlEvents: .TouchUpInside)     view.addSubview(button) }  func thumbsUpButtonPressed() {     print("thumbs up button pressed") } 
like image 140
vacawama Avatar answered Sep 16 '22 12:09

vacawama