user29000824
user29000824

Reputation: 1

swiftUI editMode not available on macOS, switch to editing view not working in final app

I followed the Apple tutorials for SwiftUI but selecting the macOS target instead of iOS (default settings). I am using Xcode Version 16.2 (16C5032a) on Sequoia (15.1.1). When reaching chapter Working with UI controls and implementing the ProfileHost view, I had the message: 'editMode' is unavailable in macOS as well as 'EditButton' is unavailable in macOS

So I tried to implement it using a normal button with a state variable. Here is the code:

struct ProfileHost: View {
    @State var editing: Bool = false
    @Environment(ModelData.self) var modelData
    @State private var draftProfile = Profile.default

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            HStack {
            if editing {
                    Button("Cancel", role: .cancel) {
                        draftProfile = modelData.profile
                        editing = false
                    }
                }
                Spacer()
                Button {
                    editing.toggle()
                } label: {
                    Label("Toggle edit", systemImage: editing ? "pencil" : "pencil.slash")
                        .labelStyle(.iconOnly)
                        .foregroundStyle(editing ? .yellow : .gray)
                }
            }
            if !editing {
            ProfileSummary(profile: modelData.profile)
            } else {
                ProfileEditor(profile: $draftProfile)
                    .onAppear {
                        draftProfile = modelData.profile
                    }
                    .onDisappear {
                        modelData.profile = draftProfile
                    }
            }
        }
        .padding()
    }
}

When testing in the preview, it works as expected, but running the app (from XCode), in editing mode only the cancel and editing buttons are displayed and in Xcode following message is given:

It's not legal to call -layoutSubtreeIfNeeded on a view which is already being laid out. If you are implementing the view's -layout method, you can call -[super layout] instead. Break on void _NSDetectedLayoutRecursion(void) to debug. This will be logged only once. This may break in the future.

Upvotes: 0

Views: 74

Answers (0)

Related Questions