Swiftui - Navigationlink Cell in a Form Stays Highlighted After Detail Pop

SwiftUI simple Pop-Up Alert from a function in a class

Here is a basic demo of activating an alert with a Button in a View body from an ObservableObject class.

struct ContentView: View {
// However you've initialized your class, it may be different than this.
@StateObject var progress = DeliveriesViewModel()

var body: some View {
VStack {
// Other views in your View body

// SwiftUI button to activate the Bool in ObservableObject class.
Button {
progress.isDisplayingAlert.toggle()
} label: {
Text("Toggle Alert")
}
}
.alert(isPresented: $progress.isDisplayingAlert) { () -> Alert in
Alert(title: Text("Alert Title"), message: Text("Body of Alert."), dismissButton: .cancel())
}
}
}

class DeliveriesViewModel: ObservableObject {

@Published var isDisplayingAlert = false

func displayAlert() {
// other function actions...

// update Published property.
isDisplayingAlert = true

// other function actions...
}
}

How to deselect a list button within a NavigationLink

The issue here is the Text which is above the List in SettingsView - a bug reported here.

Instead, you can use a native navigation title and attach it to the TabView.

struct SettingsHomeView: View {

@State var startDayNotificationSetting: String = "8:30AM"
@State var appVersion: String = "0.01"

var body: some View {
// no `Text` above `List`
List {
NavigationLink(destination: SettingsStartdayView()){
HStack {
Text("Start Day Notification")
Spacer()
Text(startDayNotificationSetting)
.font(.subheadline)
.foregroundColor(Color.gray)
.multilineTextAlignment(.trailing)
//Image(systemName: "chevron.right")
}
}
}
}
}
struct ContentView: View {

@State private var isSelectedTab = 1 // select the first tab

var body: some View {
NavigationView{
TabView(selection: $isSelectedTab) {
// ...
}
// control displaying the title depending on the `isSelectedTab`
.navigationTitle("Settings")
.navigationBarHidden(isSelectedTab == 1)
}
}
}


Related Topics



Leave a reply



Submit