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
  • 5,923
  • 9
  • 45
  • 92

4 Answers4

5

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,100
  • 9
  • 47
  • 81
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
  • 14,902
  • 4
  • 56
  • 85
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
  • 43,583
  • 14
  • 113
  • 135
0
 self.userDisplayPic.layer.cornerRadius = userDisplayPic.frame.height/2
 self.userDisplayPic.clipsToBounds = true