WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

FocusState Breaking Preview

Forums > SwiftUI

SwiftUI XCode

The following is a snippet of code I am working on. I am having trobule with. my preview after adding Focus State which I later use to swap an info button for a DONE button to dismiss the keyboard in a texteditor view. Everything works fine in the preview and my phone runs the following code fine but I can not preview the layout in xcode.

I get the following error.

MessageSendFailure: Message send failure for update

==================================

| MessageError: Connection interrupted

I have attempted to delete derived data and have reinstalled simulator devices to no avail. If I comment out all the FocusState stuff it previews fine.

import SwiftUI

struct StoryView: View {

    // MARK: - View Model
    @ObservedObject private var storyVM = StoryViewModel()

    // MARK: - Focus State
    @FocusState private var storyTextField: Bool

    // MARK: - Bindings and Variables
    @Binding var subStory: String

    var body: some View {
        ScrollView {

            // MARK: - Header
            Text("Story Section")
                .modifier(ViewHeader())

            // MARK: - Story Here
            HStack {
                // Header
                Text("STORY SECTION")
                    .modifier(PrimaryHeader())
                if storyTextField == false {
                    Button("Info \(Image(systemName: "info.circle.fill"))") {
                        subjectVM.showStoryAlert = true
                    }
                    .alert(isPresented: $subjectVM.showStoryAlert) {
                        Alert(
                            title: Text("Story Detail"),
                            message: Text("Enter story here.")
                        )
                    }.padding(.trailing)
                        .accentColor(.gray)
                } else {
                    Button("DONE \(Image(systemName: "keyboard"))") {
                        storyTextField = false
                    }
                    .font(Font.headline.weight(.heavy))
                    .padding(.trailing)
                    .accentColor(.blue)
                }
            }

            ZStack {
                // Summary text editor
                TextEditor(text: $subStory)
                    .focused($storyTextField)
                    .frame(height: 100)
                    .modifier(PrimaryTextField())
            }
        }
        .background(Color(.init(white: 0, alpha: 0.05)))
    }
}

struct SubjectView_Previews: PreviewProvider {
    static var previews: some View {
        SubjectView(subStory: .constant("Story here..."))
    }
}

1      

I'm also getting this debugger message:

2021-12-19 21:30:12.432819-0800 InfoReport[11369:3184898] [Snapshotting] Snapshotting a view (0x13400bc00, UIKeyboardImpl) that is not in a visible window requires afterScreenUpdates:YES.

1      

I'm running into this exact same thing, it just started with xcode 13.2.1 or ios 15.2 I'm not sure.

I'm seeing bugs in NavigationViews now also, so something with those new versions are just broken.

1      

I switched back to xcode 13.1 and the focus state seems to be working again in swiftUI previews

1      

I was having this issue also, and it looks like someone found an easy workaround. Mine worked after wrapping the ContentView() in a ZStack within the preview provider. Here: https://stackoverflow.com/questions/70430440/why-focusstate-crashing-swiftui-preview

6      

The workaround I found here is to wrap the preview in a ZStack, like so:

@MainActor
struct TripInfoView_Previews: PreviewProvider {
    @StateObject static var currenttrip: TripStore = TripStore.instance
    static var previews: some View {
        ZStack {
            TripInfoView(currenttrip: currenttrip, tripPartIndex: .constant(0), signalclosure: nil, readOnlyMode: .constant(false)).environmentObject(currenttrip)
        }
    }
}

I know it sounds silly, but there you go: worked for me, anyway.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED You know StoreKit, but you don’t want to do StoreKit. RevenueCat makes it easy to deploy, manage, and analyze in-app subscriptions on iOS and Android so you can focus on building your app.

Explore the docs

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.