Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI clipped Image is not really clipped

In iOS 15, the following code:

import SwiftUI
struct ContentView: View {
  var body: some View {
    VStack(spacing: 0) {
      ScrollView(.horizontal) {
        HStack{ForEach(0 ..< 10, id: \.self) {Text("Item\($0)")}}
      }
      Image(systemName: "clock").resizable()
        .padding(.top, -50).frame(height: 50)
        .contentShape(Rectangle())
        .clipped()
        .onTapGesture {print("good")}
    }
  }
}

It happens that the ScrollView can’t be scrolled because the bottom Image overlays it.
How can we scroll the ScrollView?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The clock view is eating the taps. You need to make the clock ignore taps by adding this:

.allowsHitTesting(false)

like this:

import SwiftUI
struct ContentView: View {
  var body: some View {
    VStack(spacing: 0) {
      ScrollView(.horizontal) {
        HStack{ForEach(0 ..< 10, id: \.self) {Text("Item\($0)")}}
      }
      Image(systemName: "clock").resizable()
        .padding(.top, -50).frame(height: 50)
        .contentShape(Rectangle())
        .clipped()
        .allowsHitTesting(false)
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading