代码之家  ›  专栏  ›  技术社区  ›  Ahmed Zaidan

Swift UI图像不剪裁到边界

  •  0
  • Ahmed Zaidan  · 技术社区  · 2 年前

    我有一个图片url,我使用Kingfisher库呈现。当我为图像设置maxHeight并对其进行剪裁时,我仍然可以单击帧外的图像。这就好像图像实际上没有被剪裁,但图像的其余部分被隐藏了。如何使图像与框架相一致?单击图像下方或上方的某个位置将执行打印。

    import SwiftUI
    import Kingfisher
    
    struct SwiftUIView: View {
       
       var body: some View {
           VStack {
               Spacer()
               KFImage(URL(string: "https://firebasestorage.googleapis.com:443/v0/b/hustle-85b6c.appspot.com/o/groupChats%2F4A0FFF47-A99C-4571-8EA7-014E6D2E94FB?alt=media&token=856102c8-119c-448f-9f6b-6979a540729f"))
                   .resizable()
                   .aspectRatio(contentMode: .fill)
                   .frame(maxHeight: 250)
                   .clipShape(RoundedRectangle(cornerRadius: 16))
                   .onTapGesture {
                       print("Hi")
                   }
               Spacer()
           }
       }
    }
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   sonle    2 年前

    它看起来像任何 clipped 不会阻止内容被窃听。您可能想尝试这种方法:

    KFImage(URL(string: "https://firebasestorage.googleapis.com:443/v0/b/hustle-85b6c.appspot.com/o/groupChats%2F4A0FFF47-A99C-4571-8EA7-014E6D2E94FB?alt=media&token=856102c8-119c-448f-9f6b-6979a540729f"))
       .resizable()
       .aspectRatio(contentMode: .fill)
       .frame(maxHeight: 250)
       .clipShape(RoundedRectangle(cornerRadius: 16))
       .contentShape(Rectangle()) //<- here
       .onTapGesture {
           print("Hi")
       }