iOS: frame.size.width/2 doesn't produce a circle on every device

后端 未结 6 1432
长发绾君心
长发绾君心 2020-11-29 06:09

I\'m aware that the formulae frame.size.width/2 should produce a circle border, however in XCode I am currently experiencing some discrepancies.

I have

6条回答
  •  盖世英雄少女心
    2020-11-29 06:43

    The problem is that when the circle is applied, the bounds of the view are still considered the default (320x480) and so if you have set constraints related to the screen, the image will be bigger on iPhone 6/6+ for instance.

    There are various solution, but if you want a fast one, just put your code:

    imgAvatar.layer.cornerRadius = imgFrame.frame.size.width/2
    

    in - viewDidLayoutSubviews in your view controller or in - layoutSubviews in your view, checking before if the cornerRadius is different from the width/2 of the image's frame. Something like this:

    if (imgAvatar.layer.cornerRadius != imgFrame.frame.size.width/2) {
        imgAvatar.layer.cornerRadius = imgFrame.frame.size.width/2
    }
    

提交回复
热议问题