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
Seeking an "Exit" Equivalent in Swift
Add Placeholder to Uitextfield, How to Set the Placeholder Text Programmatically in Swift
How to Color/Customize the Uiimagepickercontroller's Navigation Bar
Create a Timer Publisher Using Swift Combine
How to Insert an Image Inline Uilabel in iOS 8 Using Swift
Swift Error When Trying to Access Dictionary: 'Could Not Find Member 'Subscript''
Swift Only -- Reading from Nsinputstream
How to Setup Viewcontroller in Playgrounds
Convert Swiftui View to Nsimage
Swiftui: How to Draw Filled and Stroked Shape
How to Convert Nsurl to String in Swift
Mutate Function Parameters in Swift
Show/Hide Password - How to Add This Feature