circular image in table view cell swift
If you are creating your own imageView, it's better to set the cornerRadius inside the custom TableViewCell.
class CircularTableViewCell: UITableViewCell {
@IBOutlet weak var circularImageView: UIImageView!
override func layoutSubviews() {
circularImageView.layer.cornerRadius = circularImageView.bounds.height / 2
circularImageView.clipsToBounds = true
}
}
Note the cornerRadius property can't guarantee that the view will be absolutely round unless you set the imageView's width and height ratio to be 1:1. Another approach to create round view is using Mask.
public extension UIView {
public func round() {
let width = bounds.width < bounds.height ? bounds.width : bounds.height
let mask = CAShapeLayer()
mask.path = UIBezierPath(ovalInRect: CGRectMake(bounds.midX - width / 2, bounds.midY - width / 2, width, width)).CGPath
self.layer.mask = mask
}
}
This will allow you to call round() with any UIView and make sure the view is always round. e.g.
class CircularTableViewCell: UITableViewCell {
@IBOutlet weak var circularImageView: UIImageView!
override func layoutSubviews() {
circularImageView.round()
}
}
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 Image in rounded ImageView fit
You can wrap the image views inside container views (the cell views you already have should work) to add some padding: center the image view inside its container and constraint its width and height to about 0.75 of the container. Also you'll have to set the background and corner rounding on the container, not on the image view.
How to set image in circle in swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
image.layer.borderWidth = 1
image.layer.masksToBounds = false
image.layer.borderColor = UIColor.black.cgColor
image.layer.cornerRadius = image.frame.height/2
image.clipsToBounds = true
}
If you want it on an extension
import UIKit
extension UIImageView {
func makeRounded() {
layer.borderWidth = 1
layer.masksToBounds = false
layer.borderColor = UIColor.black.cgColor
layer.cornerRadius = self.frame.height / 2
clipsToBounds = true
}
}
That is all you need....
Related Topics
Passing Data Between View Controllers Without Segue
Show Bounding Box While Detecting Object Using Arkit 2
Present Specific View Controller in Didreceiveremotenotification with Swift
How to Add .Plist File to All Targets in Xcode
String to Double in Xcode 6's Swift
How to Automatically Create an Initializer for a Swift Class
What Is Correct Way to Notify View Controller from Appdelegate
Avcapturestillimageoutput VS Avcapturephotooutput in Swift 3
How to Add File Picker to the App on iOS 14+ and Lower
Endless Scrolling (Repeating) Background in Spritekit Game - Swift
Adding Custom Game Logic to Scene Kit (Swift)
iOS Swift Connect Wifi Programmatically and Distinguish Between Bad Password and No Wifi in Range
How to Send Different Users to Separate View Controllers Using Firebase and Xcode