-2

In my iPhone 7 simulator, my image is an exact circle. For some reason my image in iPhone 5 becomes a little squarish. This is the code that I have to make the image a circle:

    private func setImage(){
    self.profileImage.layer.borderWidth = 0.0;
    self.profileImage.layer.cornerRadius = self.profileImage.frame.size.height/2;
    self.profileImage.clipsToBounds = true
    }

There is a 1:1 aspect ratio constraint on the image. Also it works on iPhone 7 perfectly

And I add an image to show the problem in the result in iPhone 5.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Sharonica
  • 183
  • 1
  • 5
  • 14
  • 1
    before setting corner radius print image frame , height and width should be same if you want proper circle. – Chanchal Warde Feb 21 '17 at 13:12
  • (134.0, 22.0, 107.0, 107.0) This is the frame – Sharonica Feb 21 '17 at 13:15
  • Its not possible , somewhere your image frame is changing and cause this. Only one reason could be is different height and width. – Chanchal Warde Feb 21 '17 at 13:21
  • Please see my answer. The problem was that this Frame that I added was Iphone 7's frame for that image, so the height and weight were equal but it wasn't the correct frame to use for this setup. – Sharonica Feb 21 '17 at 13:26
  • Where you are calling this method? It happened with me also. Ratio not work properly. Use view hierarchy and see it property. Follow this link http://stackoverflow.com/questions/5150186/how-do-i-inspect-the-view-hierarchy-in-ios – Chanchal Warde Feb 21 '17 at 13:30
  • I solved the issue, you can see in my answer. The problem was that the frames did not yet update when I called the change (they were set to iphone 7, not to iphone 5. So adding layoutIfNeeded in the beginning of viewDidLoad made it work – Sharonica Feb 21 '17 at 13:32
  • 2
    Possible duplicate of [How to set imageView in circle like imageContacts in Swift correctly?](http://stackoverflow.com/questions/25587713/how-to-set-imageview-in-circle-like-imagecontacts-in-swift-correctly) – Aawara Feb 21 '17 at 13:39
  • Not a duplicate, since the problem was specifically in iphone 5, and the answer (that i eventually supplied), was different than what you have in the link you shared. Did you even read the answers? – Sharonica Feb 21 '17 at 13:47
  • Please do not put meta-commentary in the question (especially at the start where it doesn't make much sense) or add [solved] to the title. If you would like to put some of that material in your answer, please do - it can be found in the [revision history](https://stackoverflow.com/posts/42368265/revisions). – halfer Feb 23 '17 at 20:41
  • Downvoted for not reading the above advice. – halfer Feb 27 '17 at 12:57
  • I seriously do not understand what is the problem. I asked a question, the suggested as duplicates did NOT solve my issue, my answer solved it so I marked it as the solution. – Sharonica Feb 27 '17 at 13:26

3 Answers3

0

Let call this function in the viewDidLayoutSubviews or viewWillAppear

Danh Huynh
  • 2,337
  • 1
  • 15
  • 18
0

I found the problem. Since in storyboard I am working in iphone 7, I have to add this command in my view did load, so the frame updates the constraints for iphone 5 before making the change:

self.view.layoutIfNeeded()

I hope this helps people!

Sharonica
  • 183
  • 1
  • 5
  • 14
0

To make it work you should add

self.profileImage.layoutIfNeeded()

before :

self.profileImage.layer.cornerRadius =self.profileImage.frame.size.height/2

RimKtari
  • 261
  • 3
  • 7