How to Set Imageview in Circle Like Imagecontacts in Swift Correctly

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))

Swift Imageview Circular

Your code is making the corner radius half the width. This works fine when height == width (so radius also == height/2), but otherwise it won't work.

To fix this, add constraints to make your profileImageView square, then set the profileImageView.contentMode = .aspectFill.

How to correctly set a circle imageView with Swift?

clipsToBounds is a boolean value that determines whether subviews are confined to the bounds of the view.
Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.
Basically, this thing plays with the view's property.

Whereas masksToBounds is a boolean indicating whether sublayers are clipped to the layer’s bounds.And this thing plays with the layer of the view.

not able to prepare circular imageview using swift 2 in ios app for iPad

Try this:

@IBOutlet var imageView: UIImageView! {
didSet {
imageView.layer.borderColor = UIColor.grayColor().CGColor
imageView.layer.cornerRadius = imageView.frame.width/2
imageView.clipsToBounds = true
imageView.layer.borderWidth = 0.5
}
}

And if you want you can just set clipsToBounds in storyboard instead of using it here.

Depending on when you're calling that circularImage function you wrote, the frame on the image might not be right yet.

If this doesn't fix the issue, you probably have some constraint troubles - make sure height and width are high priority and consider deleting the top & right constraints if you're using center horizontal and vertical.

How to get circle shaped image in swift 3

My guess would be that the component you have called viewCirlce is a rectangle to start with, you are just setting the corner radius. If the component has the same width and height then this could give you a circle. If it's a rectangle, then you'll get an ellipse.

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!

How to resize image in UIView?

Welcome.
You need to either give the imageView a frame or use Autolayout to set a layout for the image inside the UIView.

so either add this at the end:

imageView.frame = view.frame

But this will not be dynamic and you should learn how to keep updating the frame whenever the superview's frame changes.

Or you can add this, instead:

imageView.translatesAutoresizingMaskIntoConstraints = false
let constraints = [
imageView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
imageView.leadingAnchor.constraint(equalTo: safeAreaLayoutGuide.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: safeAreaLayoutGuide.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)

Anyway, I really recommend you read up about AutoLayout a bit before you continue:
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/index.html
https://www.raywenderlich.com/811496-auto-layout-tutorial-in-ios-getting-started

If you find programmatic AutoLayout to be too challenging, I would recommend possibly starting with Storyboards first.



Related Topics



Leave a reply



Submit