1

If I try to open this DatePicker, the view is jumping while the transition, before coming to the fixed position. There is also a long error message in the console ..."Unable to simultaneously satisfy constraints."...

If I only display the .date component there isn't this problem. Is it a internal problem? If yes, how can I archive the same behavior because I want the date+time picker without having the detail popup screen, for faster selection.

Thanks for any help :)

https://i.stack.imgur.com/en4Im.jpg (At the second button Tap there is the buggy transition)

struct ContentView: View {
    @State var toggle = false
    @State var date = Date(timeIntervalSinceNow: 0)
    var body: some View {
        ZStack{
            Button("sdflkj"){
                toggle.toggle()
            }
            VStack{
                Spacer()
                if toggle{
                    DatePicker("lsadjf", selection: $date, displayedComponents: [.date,.hourAndMinute])
                        .labelsHidden()
                        .datePickerStyle(GraphicalDatePickerStyle())
                }
            }
        }
   
    }
}
sheldor
  • 115
  • 12

1 Answers1

0

It seems to be an error caused by overlapping the Button and DatePicker.




Tested with iPhone 12 simulator

struct ContentView: View {
    
    @State var toggle = false
    @State var date = Date(timeIntervalSinceNow: 0)
    
    var body: some View {
        ZStack{
            Button("Button"){
                // add `withAnimation()`
                withAnimation(.easeIn) {
                    toggle.toggle()
                }
            }
            .offset(y: -50) // set y offset for prevent overlapping of Button and DatePicker
            
            VStack{
                Spacer()
                if toggle {
                    DatePicker("", selection: $date, displayedComponents: [.date,.hourAndMinute])
                        .padding()
                        .labelsHidden()
                        .datePickerStyle(GraphicalDatePickerStyle())
                        .zIndex(5) // or add .zIndex for prevent overlapping of Button and DatePicker
                }
            }
        }
    }
}
Taeeun Kim
  • 964
  • 1
  • 8
  • 20
  • In the bigger App the datepicker should be shown on top of the other view like a fullscreen cover but on a sheet. Because of this I think that it is not possible that I can avoid overlapping. There I tried with ZStack and conditional statement. – sheldor Aug 17 '21 at 22:44
  • @sheldor I update my post. You can try to use `.zindex()` for that. However, the button to be dismiss will not be invisible or clickable, but there should be a button to dismiss on the `fullScreenCover`. So there would be no problem – Taeeun Kim Aug 17 '21 at 22:54
  • Ok i tested it. If doing it on the iPhone 11 simulator, there is the console error message and if doing on the iPhone 12 sim there is no message. But there is still this jumping transition which doesn't looks right. – sheldor Aug 18 '21 at 08:38
  • @sheldor For that problem, please add more information what animation problem you're talking about, add some more code, or a video of the animation you want or an animation that's happening. When I run it, the animation seems to work normally. – Taeeun Kim Aug 18 '21 at 08:52
  • I added a link here – sheldor Aug 18 '21 at 09:54
  • @sheldor thank you for the video. For that you can cover `toggle.toggle()` with `withAnimation()`. Then you can put any animation what you want. (e.g. easeIn, easeOut, spring etc.) I added the line of code on my post – Taeeun Kim Aug 18 '21 at 11:51
  • Thanks I think this workaround seems to work for now. There aren't these jumps anymore I think, but this message is still there. I accepted the answer thanks. The only problem is that there is this sound if the date picker is displayed. Perhaps you have an idea too, but for this I have a separate post. Thanks for the effort. – sheldor Aug 18 '21 at 12:01
  • @sheldor you are welcome! There doesn't seem to be a way for that, except user disabled globally sound, but I think someone might find a new way. Therefore good ask :) (https://stackoverflow.com/questions/1441849/can-i-disable-uipickerview-scroll-sound) – Taeeun Kim Aug 18 '21 at 12:07