1

I've a viewController that is presented inside a UIView as I'm using CarbonKit to make tabs like android.

I've a userProfileViewController which I'm showing under one of the tabs and this has the UIImageView which I'm making trying to show in a circle by using this code:

self.userDisplayPic.layer.cornerRadius = self.userDisplayPic.frame.size.width / 2;
self.userDisplayPic.clipsToBounds = true

and the result is: enter image description here

My guess is that it may be because of the autoresizing when presented inside a UIView might be compressing the design but other elements specially the update button seems fine. SO any suggestions how this can be fixed?

Chaudhry Talha
  • 7,231
  • 11
  • 67
  • 116

4 Answers4

6

Try to set cornerRadius in viewWillLayoutSubviews, It'll use actual width for radius.

override func viewWillLayoutSubviews() {
        self.userDisplayPic.layer.cornerRadius = self.userDisplayPic.frame.size.width / 2;
        self.userDisplayPic.clipsToBounds = true
    }
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
3

Some time self.view.layoutIfNeeded() is required

Try this code

 DispatchQueue.main.async {
         self.view.layoutIfNeeded()    

        self.userDisplayPic.layer.cornerRadius = self.userDisplayPic.frame.size.width / 2;
        self.userDisplayPic.clipsToBounds = true
        self.view.layoutIfNeeded()    


  }
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
2

It's because your self.userDisplayPic's height and width are not same first make it same then below code will be worked for you.

self.userDisplayPic.layer.cornerRadius = self.userDisplayPic.frame.size.width / 2;
self.userDisplayPic.clipsToBounds = true

And write those code in viewWillLayoutSubviews or viewWillAppear method

iPatel
  • 46,010
  • 16
  • 115
  • 137
0
 self.userDisplayPic.layer.cornerRadius = userDisplayPic.frame.height/2
 self.userDisplayPic.clipsToBounds = true