I have an image which has contentMode set to scaleAspectFit with a cornerRadius but i don't get the rounded corner but when I set the contentMode to scaleAspectFill it works. My question is i still need my contentMode to be set to scaleAspectFit and also have my cornerRadius show. Below is my sample code.
private let imageView: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.clipsToBounds = true
image.image = UIImage(named: "105")
image.layer.cornerRadius = 30
image.contentMode = .scaleAspectFit
return image
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
setUpConstraints()
}
private func setUpConstraints(){
view.addSubview(btnClose)
view.addSubview(lblRateIT)
view.addSubview(lblLoveIT)
view.addSubview(redBottomView)
view.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
imageView.topAnchor.constraint(equalTo: lblLoveIT.bottomAnchor, constant: 20),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
imageView.bottomAnchor.constraint(equalTo: redBottomView.topAnchor, constant: -20)
])
}
Without knowing what else you might be wanting to do with this, the most straight-forward approach is to use constraints that match the aspect ratio of the image itself.
So, we can:
UIViewto hold the imageViewimage.size.height / image.size.widthUsing these 3 sample images - horizontal / square / vertical:
and this example view controller:
We get this output at start (no image set):
then for Horizontal / Square / Vertical images: