Adil Ansari
Adil Ansari

Reputation: 83

Trying to make UIImageView a circle and it appears twice

I am trying to add an imageview as a circle in swift to a uiview. I call the following code in swift however it creates the image to appear briefly incorrectly before it is corrected to a circle

   override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.height / 2
      theirPicMaskFour.layer.masksToBounds = false
      theirPicMaskFour.layer.borderWidth = 0;
      theirPicMaskFour.clipsToBounds = true
  }

The following is the way it looks initially. enter image description here

The following is how it looks after. enter image description here

Upvotes: 0

Views: 124

Answers (2)

Kian
Kian

Reputation: 122

Try

theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.size.width / 2

instead of

theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.height / 2

Upvotes: 1

Saheb Roy
Saheb Roy

Reputation: 5957

Try theirPicMaskFour.layer.masksToBounds = true and log out the height before u assign just to be sure that the height isn't something out of ordinary

[EDIT] Alloc and init it in viewDidLoad but update the round rect in viewDidLayoutSubviews as it will be called multiple times, but changes on frames wont effect it as that will remain same.

Upvotes: 0

Related Questions