UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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..."))
    }
}

3      

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.

3      

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.

3      

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

3      

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

8      

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.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.