0

See this simple example. Putting DatePicker in VStack has weird layout issue and causes all the content in VStack to not show up (I'm using XCode 13.2 + iOS15.2).

struct ContentView: View {
    @State var date: Date = Date()
    
    var body: some View {
        Form {
            VStack {
                DatePicker("Date", selection: $date)
                Text("date: \(date)")
            }
        }
    }
}

Replacing the DatePicker with other views (e.g. TextField) works fine. Removing the VStack also works fine. However, I need to have the VStack to make subviews inside it to show up in a single cell.

I googled but wasn't able to find any discussion about this issue. I tried to wrap DatePicker in HStack or ZStack but neither worked. Does anyone know how to work around this issue? Thanks.

rayx
  • 1,329
  • 10
  • 23
  • 1
    Unable to duplicate the issue. It seems to be working fine. Xcode 13.3, iOS 15.4 simulator. – Yrb Mar 26 '22 at 14:32
  • Thanks for the information. I'll upgrade my system tomorrow and try it. BTW, does it mean that, if I submit my app containing code like this, it will work on iOS15.4 but not on iOS15.2? If so, that seems an issue because user may still use iOS15.2. – rayx Mar 26 '22 at 14:41
  • 1
    I have never had a problem with that setup. I suspect there is some issue within you other code somewhere. We can't tell from what you posted. I would try running it in a newly created project BEFORE upgrading to see if the problem exists. It may have been a one off bug in the version you are using. – Yrb Mar 26 '22 at 14:46
  • Thanks! It's a simulator issue. Please see my answer below. – rayx Mar 27 '22 at 01:46

1 Answers1

0

A big thank to @Yrb, whose suggestion helps me to find the root cause of the issue.

TL;DR It's a simulator issue. Resetting the simulated device resolved the issue.

More details: I first noticed the issue when I tested my app on the simulator. I thought it was a bug with iOS, especially after I was able to reproduce the issue with the above code in a fresh project. Since @Yrb wasn't able to reproduce the issue, I was about to upgrade my system. However, his suggestion reminded me to try it again on other simulator (I usually use iPhone SE2). It was surprising that other devices didn't have the issue. What's more, while I could reproduce the issue consistently on iPhone SE2 simulator, the preview in XCode just worked fine. So I reset the iPhone SE2 simulator and restarted it (BTW, just restarting it didn't work), the code worked fine!

It's still a mystery what exactly happened in the simulator. But I'm glad to know it's neither my code issue nor an iOS issue.

It took me more than half a day to investigate the issue :( Lessons learned when running into similar weird issue in future: 1) reset simulator 2) try it on physical device also.

rayx
  • 1,329
  • 10
  • 23