In Swiftui How to Set the Environment Variable of Editmode in an Xcodepreview

Merely declaring the environment variable openURL cause popover behavior to go wonky

It appears toolbar defect... using navigationBarItems works fine.

Tested with Xcode 12.1 / iOS 14.1

struct PopoverView: View {
@State var isShowingPopover = false
@Environment(\.openURL) var openURL

var body: some View {
Text("Content")
.navigationBarItems(trailing:
Button("Popover") {
isShowingPopover.toggle()
}
.popover(isPresented: $isShowingPopover) {
Button(action:{
openURL(URL(string: "https://cnn.com")!)
}){
Label("Open CNN", systemImage: "hand.thumbsup")
}
.padding()
})
}
}

How to localize EditButton in SwiftUI?

Create your own EditButton might be the way to go for now

struct NewEditButton: View {
@Binding var editMode: EditMode
var onDone: (() -> Void)?

var body: some View {
Button(action: {
if self.editMode.isEditing {
self.editMode = .inactive
self.onDone?()
} else {
self.editMode = .active
}
}) {
self.editMode == .active ? Text(NSLocalizedString("active", comment: "active")) : Text(NSLocalizedString("edit", comment: "edit"))
}
}
}

and then the external editMode state can be used to set the current editing model In SwiftUI how do I set the environment variable of editMode in an XcodePreview

SwiftUI @Binding update doesn't refresh view

You have not misunderstood anything. A View using a @Binding will update when the underlying @State change, but the @State must be defined within the view hierarchy. (Else you could bind to a publisher)

Below, I have changed the name of your ContentView to OriginalContentView and then I have defined the @State in the new ContentView that contains your original content view.

import SwiftUI

struct OriginalContentView: View {
@Binding var isSelected: Bool

var body: some View {
Button(action: {
self.isSelected.toggle()
}) {
Text(isSelected ? "Selected" : "Not Selected")
}
}
}

struct ContentView: View {
@State private var selected = false

var body: some View {
OriginalContentView(isSelected: $selected)
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}


Related Topics



Leave a reply



Submit