使用SwiftUI(Xcode 11.0 beta 2)进行实验,我尝试用图像填充视图:
Image("large")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 80, height: 80, alignment: .center)
.border(Color.black)
这样呈现:
我想应用类似于UIView.clipsToBounds
的东西,以使图像被裁剪并且框外的部分不可见。
答案 0 :(得分:4)
您可以使用.clipped()
修饰符来制作与UIView.clipsToBounds
类似的东西
Image("large")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 80, height: 80, alignment: .center)
.border(Color.black)
.clipped() // Equal to clipsToBounds = true
答案 1 :(得分:1)
答案 2 :(得分:-1)
Use GeometryReader can fix the issue where if the clipped region of the image overlaps a button, that button will NOT work
like this:
GeometryReader { geo in
Image("large")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 80, height: 80, alignment: .center)
.border(Color.black)
}.frame(width: 150, height: hh)