I could not figure out how to resize the images in the buttons. I used the code from the resizedImage function from MyLocations, but I just can’t figure out why it is not working.
I added this extension:
extension UIImage {
func resizedImage(withBounds bounds: CGSize) -> UIImage {
let horizontalRatio = bounds.width / size.width
let verticalRatio = bounds.height / size.height
let ratio = min(horizontalRatio, verticalRatio)
let newSize = CGSize(width: size.width * ratio,
height: size.height * ratio)
UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
draw(in: CGRect(origin: CGPoint.zero, size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Then I added these properties to the button.imageView
button.imageView?.clipsToBounds = true
button.imageView?.contentMode = .scaleAspectFill
let resizedImage = image.resizedImage(withBounds: CGSize(width: 60.0, height: 60.0))
button.setImage(resizedImage, for: .normal)
But it is not seeming to do anything to the non-square images