swiftui

swiftUI bottomBar toolbar disappears when going back

余生颓废 提交于 2021-01-05 08:53:09
问题 I have these swiftUI views and trying to use the toolbar (bottomBar). When you launch the app it appears fine, but after going to View2 using he navigationLink and then go back to the main view the toolbar disappears. It happens when the NavigationLink being inside the list. If you don't use a list (put the navigation link inside a VStack or similar) it works as expected and the toolbar reappears when you go back to the initial view. Is there a way to fix this? import SwiftUI struct View2:

SwiftUI: respond to app termination on macOS

那年仲夏 提交于 2021-01-05 08:52:15
问题 I am creating a cross-platform app with SwiftUI 2.0. The app's lifecycle is managed by SwiftUI, so there is no app or scene delegate. If possible, I would like to keep it that way. In order to persist data when the app quits or enters the background, I am watching for changes on scenePhase . @main struct MyApp: App { @Environment(\.scenePhase) var scenePhase @StateObject var dataModel = DataModel() var body: some Scene { WindowGroup { ContentView() .onChange(of: scenePhase) { newScenePhase in

SwiftUI Custom TextField with UIViewRepresentable Issue with ObservableObject and pushed View

混江龙づ霸主 提交于 2021-01-05 08:42:52
问题 I created a UIViewRepresentable to wrap UITextField for SwiftUI, so I can e.g. change the first responder when the enter key was tapped by the user. This is my UIViewRepresentable (I removed the first responder code to keep it simple) struct CustomUIKitTextField: UIViewRepresentable { @Binding var text: String var placeholder: String func makeUIView(context: UIViewRepresentableContext<CustomUIKitTextField>) -> UITextField { let textField = UITextField(frame: .zero) textField.delegate =

SwiftUI Widget background based on the value passed image url or gradient background

北城余情 提交于 2021-01-05 07:38:27
问题 What I would like to do is give the user the option to choose whether the widget background is an image taken from http or a gradient background . I currently have the following note structure, but I can't get it to work. So typeBg must have a default value, if not passed it should take the default value. The values of image and bgColors must be optional parameters. struct Note: Identifiable, Codable { let title: String let message: String let image: String? let bgColors: [Color?]//[String?]

SwiftUI mode.wrapped.value() lifts scrollView

戏子无情 提交于 2021-01-05 07:25:06
问题 As it's common practice to use, I have LazyVStack inside my scroll View. I have 3 views. 1st view contains Header which can be shown or hidden, and all other content goes under the header 2nd view with button to present 3rd view (when 3rd view is presented I want header to disappear, it does, but there is blank space under, and when I come back to previous view with mode.wrappedValue.dismiss() it's lifted and I cannot access the last image from 2nd view. My repo: https://github.com

Write unit tests for ObservableObject ViewModels with Published results

北城以北 提交于 2021-01-05 07:23:25
问题 Today again one combine problem I currently run in and I hope that someone of you can help. How can normal unit tests be written for ObservableObjects classes which contain @Published attributes? How can I subscribe in my test to them to get the result object which I can assert? The injected mock for the web service works correctly, loadProducts() function set exactly the same elements from the mock in the fetchedProducts array. But I don't know currently how to access this array in my test

Write unit tests for ObservableObject ViewModels with Published results

淺唱寂寞╮ 提交于 2021-01-05 07:23:08
问题 Today again one combine problem I currently run in and I hope that someone of you can help. How can normal unit tests be written for ObservableObjects classes which contain @Published attributes? How can I subscribe in my test to them to get the result object which I can assert? The injected mock for the web service works correctly, loadProducts() function set exactly the same elements from the mock in the fetchedProducts array. But I don't know currently how to access this array in my test

SwiftUI - Localization of a dynamic Text

浪尽此生 提交于 2021-01-05 07:20:25
问题 I am struggling with the locilization of some of my TextFields. Usually a "normal" localization of a Text() or TextField() works without any problem in my App if the text I want to translate is hardcoded like this: Text("English Text") I translate it in my Localizable.strings like this: "English Text" = "German Text"; Now I want to translate TextFields which are more dynamic, but where I know each possible entry: TextField("New note" + (refresh ? "" : " "),text: $newToDo, onCommit: { self

SwiftUI - Localization of a dynamic Text

北慕城南 提交于 2021-01-05 07:19:09
问题 I am struggling with the locilization of some of my TextFields. Usually a "normal" localization of a Text() or TextField() works without any problem in my App if the text I want to translate is hardcoded like this: Text("English Text") I translate it in my Localizable.strings like this: "English Text" = "German Text"; Now I want to translate TextFields which are more dynamic, but where I know each possible entry: TextField("New note" + (refresh ? "" : " "),text: $newToDo, onCommit: { self

Why does the size animate and not the text with this SwiftUI view?

社会主义新天地 提交于 2021-01-05 07:10:13
问题 Why does the size animate and not the text with this SwiftUI view? The Text correctly gets bigger and smaller, but the value of the text does not change. struct ContentView: View { @State private var isAnimating = 0 var foreverAnimation: Animation { Animation.linear(duration: 1) .repeatForever() } var body: some View { Text(isAnimating > 5 ? "Hello" : "Byebye") .scaleEffect(isAnimating > 5 ? 1.5 : 1) .animation(foreverAnimation) .onAppear { self.isAnimating = 10 } } } 来源: https:/