SwiftUI图像clipsToBounds

时间:2019-06-30 13:40:53

标签: ios swift swiftui

使用SwiftUI(Xcode 11.0 beta 2)进行实验,我尝试用图像填充视图:

Image("large")
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(width: 80, height: 80, alignment: .center)
    .border(Color.black)

这样呈现:

No clipping

我想应用类似于UIView.clipsToBounds的东西,以使图像被裁剪并且框外的部分不可见。

3 个答案:

答案 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)

select title from blog where title like '%stackoverflow%', 

OutPut

答案 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)