AnswerBun.com

Make a circular button programatically

Stack Overflow Asked by user14469311 on December 19, 2020

I want to create a circular UIButton in my iOS application.

The button is for creating a profile picture for the user.

This is the circular profile picture:

And this is how it looks like after a picture was chosen:
enter image description here

You can see that the button is too big and not circular.
This is my code:

func setUpProfilePicture() {
        profileIcon = UIImage(named: "characteer")!
        profilePicture.setImage(profileIcon, for: .normal)
        profilePicture.contentMode = .scaleAspectFill
        profilePicture.addTarget(self, action: #selector(handleSelectedPhoto), for: .touchUpInside)
        
        self.view.addSubview(profilePicture)
        
        profilePicture.translatesAutoresizingMaskIntoConstraints = false
        profilePicture.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        profilePicture.topAnchor.constraint(equalTo: view.topAnchor, constant: -180).isActive = true
        profilePicture.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 50).isActive = true
        profilePicture.heightAnchor.constraint(equalTo: view.heightAnchor, constant: 50).isActive = true
    }
  

The chosen picture should be just filled in the circle of the profile picture character image. I worked with auto layouts, so the most tutorials I found didn’t help me!

Thank you! :)

3 Answers

Adjust UIButton's layer:

profilePicture.imageView.contentMode = .scaleAspectFit
let cornerRadius = 25 // 50 * 0.5
profilePicture.layer.cornerRadius = cornerRadius
profilePicture.layer.masksToBounds = true

Correct answer by siarheibrazil on December 19, 2020

Programmatically:

private extension UIView { 
    func willCircle() {
    self.layer.cornerRadius = min(self.frame.size.height, self.frame.size.width) * 0.5
    self.layer.masksToBounds = true
    self.clipsToBounds = true
    }
}
  • usage: profilePicture.willCircle

Storyboard:

@IBInspectable
var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
        layer.masksToBounds = newValue > 0
    }
}

Answered by DrainOpener on December 19, 2020

You will need to change the corner radius of your UIButton to be half of it's size

profileIcon.imageView?.contentMode = .scaleAspectFit
profileIcon.layer.cornerRadius = min(profileIcon.frame.height, profileIcon.frame.width) / 2
profileIcon.layer.masksToBounds = true

Answered by אורי orihpt on December 19, 2020

Add your own answers!

Related Questions

Get data from DataGrid if DataGridCheckBoxColumn is checked

0  Asked on November 4, 2021 by user12711263

     

The value of local variable isn’t used

1  Asked on November 4, 2021 by greenycodeskii

     

clickhouse : information_schema.KEY_COLUMN_USAGE

1  Asked on November 4, 2021 by sravya-jasthi

 

WPF prevent combobox selection change under condition

1  Asked on March 8, 2021 by hasan-h

     

How to listen to Boot Complete broadcast in Oneplus?

1  Asked on March 8, 2021 by saarang-tiwari

 

Indirection requires pointer operand error

1  Asked on March 8, 2021 by mr-needhelp

 

Point Cloud Segmentation

1  Asked on March 7, 2021 by aldi-lin

   

Angular HTTP get request is not working in ngOnInit

1  Asked on March 6, 2021 by iamgrooot

   

CheckedChanged not triggering

0  Asked on March 6, 2021

     

flutter splash screen is blurry on physical device

1  Asked on March 5, 2021 by mohamed-mostapha

   

Nginx syslog post request

1  Asked on March 5, 2021 by andreas-hunter

         

Cloning Objects in Google Optimize

0  Asked on March 4, 2021 by jpfotoz

     

Specialize how std::vector grows

3  Asked on March 3, 2021 by gary-allen

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir