swiftui

SwiftUI authentication view

旧街凉风 提交于 2020-12-15 07:20:48
问题 In swift UI I want the content view to be my root view for my app with a conditional setup to check if the users is logged in or not. If the user is logged in a list view shows other wise show the login view so the user can log in. Based on my research i could not find a best way to do this. In my case I can not get the solution I found to work and do not know if it is the best solution. import SwiftUI struct ContentView: View { @ObservedObject var userAuth: UserAuth = UserAuth() // MARK: -

NavigationLink hides the Destination View, or causes infinite view updates

爷,独闯天下 提交于 2020-12-15 07:08:52
问题 Let us consider the situation when you have ContentView and DestinationView . Both of them depend on some shared data, that typically lies inside the @ObservedObject var viewModel , that you pass from parent to child either via @EnvironmentObject or directly inside init() . The DestinationView in this case wants to enrich the viewModel by fetching some additional content inside .onAppear . In this case, when using NavigationLink you might encounter the situation when the DestinationView gets

NavigationLink hides the Destination View, or causes infinite view updates

折月煮酒 提交于 2020-12-15 07:07:40
问题 Let us consider the situation when you have ContentView and DestinationView . Both of them depend on some shared data, that typically lies inside the @ObservedObject var viewModel , that you pass from parent to child either via @EnvironmentObject or directly inside init() . The DestinationView in this case wants to enrich the viewModel by fetching some additional content inside .onAppear . In this case, when using NavigationLink you might encounter the situation when the DestinationView gets

Current font used in a Text object in SwiftUI

烂漫一生 提交于 2020-12-15 07:02:50
问题 Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font() does not work. 回答1: The current font is accessible from the environment: struct ChildView: View { @Environment(\.font) var font var body: some View { Text("Italic version of the hierarchy's font") .font((font ?? .body).italic()) } } See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy. 回答2: You can use systemFont.

Current font used in a Text object in SwiftUI

不羁岁月 提交于 2020-12-15 07:02:14
问题 Is there a way to get/obtain the current font used in a Text object in SwiftUI? ex. Text("abc").font() does not work. 回答1: The current font is accessible from the environment: struct ChildView: View { @Environment(\.font) var font var body: some View { Text("Italic version of the hierarchy's font") .font((font ?? .body).italic()) } } See https://developer.apple.com/documentation/swiftui/environmentvalues for the full list of available keys, they can come in handy. 回答2: You can use systemFont.

SwiftUI NavigationView, going back if NavigationLink is inside a NavigationBarItem

ε祈祈猫儿з 提交于 2020-12-15 06:48:46
问题 As there are some problems with iOS 13.4 and Xcode 11.4 with presentationMode.wrappedValue.dismiss() I am looking for an alternative approach to go back programmatically. I found this solution from MScottWaller: iOS SwiftUI: pop or dismiss view programmatically Unfortunately, in my case it does not work: struct MasterView: View { @State private var showDetail = false var body: some View { VStack { Text("MasterView") .navigationBarItems(trailing: HStack { NavigationLink(destination: DetailView

SwiftUI NavigationView, going back if NavigationLink is inside a NavigationBarItem

女生的网名这么多〃 提交于 2020-12-15 06:48:29
问题 As there are some problems with iOS 13.4 and Xcode 11.4 with presentationMode.wrappedValue.dismiss() I am looking for an alternative approach to go back programmatically. I found this solution from MScottWaller: iOS SwiftUI: pop or dismiss view programmatically Unfortunately, in my case it does not work: struct MasterView: View { @State private var showDetail = false var body: some View { VStack { Text("MasterView") .navigationBarItems(trailing: HStack { NavigationLink(destination: DetailView

SwiftUI - Change of Binding forces NavigationView to go back automatically

折月煮酒 提交于 2020-12-15 06:32:40
问题 I'm currently encountering problems when changing a binded value inside of a navigation link. The data structure looks like this: internal struct DailyNutritionReportElement: Identifiable, Hashable, Codable { internal var id: UUID internal var amount: Double internal var comment: String internal var dailyNutritionReportMO: DailyNutritionReport? internal var dailyNutritionReportElementTypeMO: DailyNutritionReportElementType internal var isDone: Bool } In my main view I then show different

SwiftUI - Change of Binding forces NavigationView to go back automatically

耗尽温柔 提交于 2020-12-15 06:31:51
问题 I'm currently encountering problems when changing a binded value inside of a navigation link. The data structure looks like this: internal struct DailyNutritionReportElement: Identifiable, Hashable, Codable { internal var id: UUID internal var amount: Double internal var comment: String internal var dailyNutritionReportMO: DailyNutritionReport? internal var dailyNutritionReportElementTypeMO: DailyNutritionReportElementType internal var isDone: Bool } In my main view I then show different

How do you access & edit an @AppStorage var from multiple other views in SwiftUI 2.0?

风流意气都作罢 提交于 2020-12-15 06:23:10
问题 Is there a better way to do this? Is there a way to access the UserDefaults in the environment?? I did the following: struct ContentView: View { @AppStorage("darkMode") var darkMode = false var body: some View { SubView(darkMode: $darkMode) } } } struct SubView: View { @Binding var darkMode: Bool var body: some View { Text("Dark Mode is \(darkMode == true ? "on" : "off")") } } 回答1: By using @AppStorage in different views you still access the same UserDefaults.standard storage (unless you