3

I'm new to SwiftUI and trying to implement Piker view from SwftUI.

I have simple Picker with a few elements in it to display. I set a frame with to 50 only, added border to it and applied view modifier .clipped(). Heres code:

import SwiftUI

struct ContentView: View {
var body: some View {
        VStack {
            Picker(selection: .constant(1), label: Text("")) {
                Text("1").tag(1)
                Text("2").tag(2)
                Text("3").tag(3)
            }
            //.clipped()
            .labelsHidden()
            .frame(width: 50)
            .clipped()
        }
        .border(Color.red)
   }
}

Everything looks ok. But, the problem start to occur when run the app and at the moment of running just open Debug View Hierarchy and you'll see like the actual size if Picker width is much bigger and I don't know why is that happening. Please, if you know how to fix it, I'll appearance it a lot, thank you 🙏

enter image description here

2
  • 1
    Does this answer your question stackoverflow.com/questions/63497649/…? Actually why it is a problem for you, it is internal representation? Commented Jan 8, 2021 at 14:25
  • thanks but it didn't work with iOS 14 and Xcode 12.3. This is the problem to me because when I combine it with other UI elements then it will block the interaction with other UI Commented Jan 8, 2021 at 14:41

1 Answer 1

3

Try with content shape, it should restrict picker hit testing only to clipped area, like

VStack { 
    Picker(selection: .constant(1), label: Text("")) { 
          Text("1").tag(1) 
          Text("2").tag(2) 
          Text("3").tag(3) 
     } //.clipped() 
     .labelsHidden()
     .frame(width: 50) 
     .clipped()
     .contentShape(Rectangle())   // << here !!
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help, it works like a charm) I actually did't know that in SwiftUI we can define interacting area of the view, pretty cool 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.