11

Okay, I wanted to know why my preview wasn't working after updating Xcode. So I have an enum that looks like this.

enum Field {
    case email
    case securedPassword
    case unsecuredPassword
}

Now when I add @FocusState to my TestView my preview just crashes and doesn't update. Here is how my code looks like.

struct TestView1: View {
    @FocusState var focusedField: Field?
    
    var body: some View {
        Color.blue
    }
}

Now when I comment out the @FocusState I am able to change the color to something like red and the preview updates. When the @FocusState is not commented out, when I change the color to something new it doesn't update the preview and gives me a weird crash.

Now is this a bug and if so is there a work around?

Luis Ramirez
  • 966
  • 1
  • 8
  • 25

1 Answers1

46

Please see my related answer I posted a couple of months ago on the Apple Developer Forums located at: https://developers.apple.com/forums/thread/681571?answerId=690251022#690251022 . Does this work for you?

struct TestView1: View {
    enum Field: Hashable {
        case email
        case securedPassword
        case unsecuredPassword
    }

    @FocusState var focusedField: Field?
    
    var body: some View {
        Form {
            Color.blue
        }
    }
}

struct TestView1_Previews: PreviewProvider {
    static var previews: some View {
        // Here we've wrapped `TestView1` in a `ZStack { ... }` View
        // so that it won't be the top-level View in our Preview, to avoid
        // the known bug that causes the `@FocusState` property of a
        // top-level View rendered inside of a Preview, to not work properly.
        ZStack {
            TestView1()
        }
    }
}
Jeremy Pearson
  • 666
  • 1
  • 8
  • 8
  • 2
    Yeah that did it by nesting the TestView1 into a ZStack inside the preview. Thanks. – Luis Ramirez Dec 22 '21 at 03:23
  • 1
    You're very welcome, @LuisRamirez. I'm glad I could help :)! I'm pretty sure I did report this bug to Apple, but it *might* have been as a part of my bug report I submitted to them about the on-screen keyboard not showing in SwiftUI Preview's run in the Xcode Previews app on a physical iPhone after tapping on a TextField, until the app has first been sent into the background and then brought back into the foreground... – Jeremy Pearson Dec 22 '21 at 05:19
  • Decent workaround for something needs to addressed by Apple. Has anyone reported this to Apple Feedback? https://feedbackassistant.apple.com – Jack Dec 22 '21 at 18:56
  • @Jack, I think that I did report it to Apple via their feedback assistant tool, but I'm not 100% sure I did (if I did it would have been months ago). Having said that, I've read repeatedly online people claiming that Apple encourages multiple people to report the same bugs to them, as that assists them with knowing how they ought to prioritise bug fixes based on what's most important to the development community. But I don't know if that claim is true or is just a false rumour. So feel free to report it yourself to Apple if you'd like to :). – Jeremy Pearson Dec 23 '21 at 04:57
  • 1
    @JeremyPearson Thanks a ton!!, submitted a feedback fingers crossed – user1046037 Dec 23 '21 at 07:01
  • 1
    Thanks! I was going a little crazy trying to figure out what I was doing wrong :). I also submitted feedback. Hopefully a lot of people are reporting this. – carl_h Dec 27 '21 at 14:47
  • 1
    Adding the ZStack in the preview fixed the issue for me. Thanks – MACE Jan 13 '22 at 00:50