Ios: Frame.Size.Width/2 Doesn't Produce a Circle on Every Device

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

You can create an extension to apply the mask and border straight to your image so it will always work disregarding the screen size / scale:

edit/update:

Xcode 11 • Swift 5 or later

extension UIImage {
var circleMask: UIImage? {
let square = CGSize(width: min(size.width, size.height), height: min(size.width, size.height))
let imageView = UIImageView(frame: .init(origin: .init(x: 0, y: 0), size: square))
imageView.contentMode = .scaleAspectFill
imageView.image = self
imageView.layer.cornerRadius = square.width/2
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.borderWidth = 5
imageView.layer.masksToBounds = true
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale)
defer { UIGraphicsEndImageContext() }
guard let context = UIGraphicsGetCurrentContext() else { return nil }
imageView.layer.render(in: context)
return UIGraphicsGetImageFromCurrentImageContext()
}
}

imgAvatar.image = yourImage.circleMask

Image View is not a perfect circle

It seems that you have an issue with the layout-subviews of the cell - you need a few more cycles of the auto-layout mechanism

You can solve it by overriding layoutSubViews and put the snip code you wrote in there.

You should override this method only if the autoresizing and
constraint-based behaviors of the subviews do not offer the behavior
you want. You can use your implementation to set the frame rectangles
of your subviews directly.

UIView not shows round

use this...

    func makeCircle (view: UIView) {
view.clipsToBounds = true
let height = view.frame.size.height
let width = view.frame.size.width
let newHeight = min(height, width) // use "max" if you want big circle

var rectFrame = view.frame
rectFrame.size.height = newHeight
rectFrame.size.width = newHeight
view.frame = rectFrame
view.layer.cornerRadius = newHeight/2
}

use like this:

@IBOutlet var rectView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

makeCircle(view: rectView)
}

Image in circle not working in iPhone 5 (works in iPhone 7)

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!

ios10: viewDidLoad frame width/height not initialized correctly

The most common issues you describe are appearing in iOS 10 only and can be solved by adding this line (if necessary):

self.view.layoutIfNeeded()

just above the code, that is responsible for changing constraint, layer.cornerRadius etc.

OR

place your code related to frames / layers into viewDidLayoutSubviews() method:

override func viewDidLayoutSubviews() {

super.viewDidLayoutSubviews()
view.layer.cornerRadius = self.myView.frame.size.width/2
view.clipsToBounds = true

... etc
}

Make Circle of a UIView which is of different width and height

As per @sebastienFCT suggestions, I applied this logic to get (little closer) circle shape for myView.

CGFloat max = MAX(myView.frame.size.width, myView.frame.size.height);
CGFloat min = MIN(myView.frame.size.width, myView.frame.size.height);
CGFloat diff = max - min;
CGFloat borderWidth = max - (diff/2.f);
myView.frame = CGRectMake(myView.frame.origin.x, myView.frame.origin.y, borderWidth, borderWidth);
myView.layer.cornerRadius = borderWidth/2.f;
myView.layer.masksToBounds = YES;

I know it's not a proper way, but for now it helped me to send by first build to the client and made my boss happy!

View changes size depending on device screen size, but should have a fixed size

So I guess you used Xcode's “Reset to Suggested Constraints” option, like this:

reset to suggested constraints

When you do that, Xcode guesses what constraints you want. Unfortunately, in your case, it guessed wrong. It did create the centering constraints you wanted, but it did not create the width and height constraints you wanted. Instead it created leading edge and top edge constraints, like this:

edge constraints

So when you load your scene on a larger device, in order to satisfy those constraints, auto layout has to make the view larger, like this:

resized

To fix this, you need to delete the edge constraints:

deleting edge constraints

And add width and height constraints:

adding width and height constraints

So your final constraints on the subview look like this:

final constraints

With these constraints, when you load your scene on a larger device, the subview will stay centered and not change size:Sample Image

View changes size depending on device screen size, but should have a fixed size

So I guess you used Xcode's “Reset to Suggested Constraints” option, like this:

reset to suggested constraints

When you do that, Xcode guesses what constraints you want. Unfortunately, in your case, it guessed wrong. It did create the centering constraints you wanted, but it did not create the width and height constraints you wanted. Instead it created leading edge and top edge constraints, like this:

edge constraints

So when you load your scene on a larger device, in order to satisfy those constraints, auto layout has to make the view larger, like this:

resized

To fix this, you need to delete the edge constraints:

deleting edge constraints

And add width and height constraints:

adding width and height constraints

So your final constraints on the subview look like this:

final constraints

With these constraints, when you load your scene on a larger device, the subview will stay centered and not change size:Sample Image

How to set imageView in circle like imageContacts in Swift correctly?

What frame size are you using for image? I can get a perfect circle if I set the frame to be a square.

let image = UIImageView(frame: CGRectMake(0, 0, 100, 100))

Make images round in swift?

It's displaying a diamond shape because you're setting the cornerRadius before the the size of the view changes.

This would result in a diamond shape:

var myView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
myView.backgroundColor = UIColor.redColor()
view.addSubview(myView)
myView.layer.cornerRadius = myView.frame.size.width / 2
// setting frame doesn't change corner radius from the former large value
myView.frame = CGRect(x: 50, y: 50, width: 50, height: 50)

You can set this immediately before the view is displayed by doing so in viewWillAppear:



Related Topics



Leave a reply



Submit