Experimenting with SwiftUI (Xcode 11.0 beta 2), I try to fill a View with an image :
Image("large") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 80, height: 80, alignment: .center) .border(Color.black)
This renders like so :
I would like to apply something similar to UIView.clipsToBounds
so the image is clipped and the parts outside of the box are not visible.
Image("name"). resizable(). scaledToFit() isn't bugged, though. So you can wrap your image in a view, adjust the frame of the view to whatever size you need, then scaledToFit() will then make the image as big as it can be while keeping aspect ratio.
SwiftUI lets you clip any view to control its shape, all by using the clipShape() modifier. The Circle clip shape will always make circles from views, even if their width and height are unequal – it will just crop the larger value to match the small.
In other words, clipped() applies a mask equivalent to the bound frame "rectangle", thus hiding anything that goes beyond that rectangle. SwiftUI comes with two clipped() alternatives: cornerRadius(_:) and clipShape(_:style) .
You can use the .clipped()
modifier, which results in an effect similar to UIView.clipsToBounds
:
Image("large") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 80, height: 80, alignment: .center) .border(Color.black) .clipped() // Equal to clipsToBounds = true
Image("large") .resizable() .clipShape(Circle()) .frame(width: 200.0, height: 200.0) .overlay(Circle().stroke(Color.white,lineWidth:4).shadow(radius: 10))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With