0

I tried to round the photo UIImageView as follows:

in viewDidLoad() :

    photo = UIImageView()
    photo.image = UIImage(named: "head")?.withRenderingMode(.alwaysTemplate)
    topView.addSubview(photo) 
    photo.snp.makeConstraints{ make in
        make.top.equalToSuperview().offset(gap*3)
        make.height.width.equalTo(80)
        make.centerX.equalToSuperview()
        }
override func viewWillLayoutSubviews() {
        photo.makeRounded()
    }
extension UIImageView {
    func makeRounded() {
        self.layer.cornerRadius = self.frame.height / 2
        self.layer.borderWidth = 2
        self.layer.masksToBounds = false
        self.layer.borderColor = UIColor.customyellow.cgColor
        self.clipsToBounds = true
    }
}

But when I switch to this view on the simulator, a square frame will appear and then change to a circle. Is there any way to solve this? Thank you!

EDIT: I solved this using DispatchQueue. Thanks to this comment on another post. How to set image in circle in swift

guckmalmensch
  • 973
  • 2
  • 9
  • 22

2 Answers2

0

Call the method photo.makeRounded() in viewDidLoad() instead of in viewWillLayoutSubviews()

-1

photo = UIImageView().makeRounded()

cbiggin
  • 1,942
  • 1
  • 17
  • 18