swiftui

How to make the View update instant in SwiftUI?

六月ゝ 毕业季﹏ 提交于 2021-02-05 08:38:07
问题 I have a Text that acts as a timer with different values of my array. And it displays with timerIntervalSinceNow . The problem is when it reaches zero it should show zero to but that does not happen before clicking again on the screen. If I do not click on it it keeps going up since it is timeIntervalSinceNow , but when I click it switches to the Text with 0 sec . Any way on telling SwiftUI to do this by itself without clicking? if(exerciseTime[value].timeIntervalSinceNow > 0) { Text

Ternary operator inside NavigationLink SwiftUI

ぃ、小莉子 提交于 2021-02-05 08:24:11
问题 I have a problem when trying to user ternary operator on a button in NavigationLink. I have an array of campaigns, which is displayed in a carouselView. Bellow carousel there is a button(NavigationLink) for opening another view which displays details of the campaign. The array of campaigns is empty initially, so I have to check if self.cardCampaigns > 0, if that is true it should navigate to a view which displays details of the campaign, otherwise ignore( I am trying to show a view with text

SwiftUI @FetchRequest crashes the app and returns error

本小妞迷上赌 提交于 2021-02-05 07:51:29
问题 I am trying to use core data within my mac app with SwiftUI using Xcode 11. I have checked "Using Core Data" when creating the project. I also have created the entity (called VisitedCases) and used editor to create NSManagedObject Subclass files. I also have set the Codegen to Manual/none. Here is the code in the generated NSManagedObject files: VisitedCases+CoreDataProperties.swift extension VisitedCases { @nonobjc public class func fetchRequest() -> NSFetchRequest<VisitedCases> { return

Swift: NavigationLink calling destination's init method multiple times

时光总嘲笑我的痴心妄想 提交于 2021-02-05 07:50:48
问题 I have two classes SubmitPhoneView and VerifyPhoneView. For some reason, I noticed that whenever I input a digit into the textfield of SubmitPhoneView, it calls the init method of VerifyPhoneView. I want it to only be called once (when I press the continue button on SubmitPhoneView) Why would this be? Class SubmitPhoneView: import SwiftUI import Firebase struct SubmitPhoneView: View { @State private var phoneNumber: String = "" @State private var verificationID : String = "" @State private

SwiftUI Navigation through multiple screens

淺唱寂寞╮ 提交于 2021-02-05 07:49:26
问题 I'm a bit confused on how navigation works in SwiftUI. Does only the view starting the navigation need a NavigationView ? I have one view with a NavigationView that has a NavigationLink to a second view. The second view then has a NavigationLink to a third and final view. However, when my second view navigates to my third view, I get this message in the logs: unbalanced calls to begin/end appearance transitions for <_TtGC7SwiftUI19UIHostingControllerVS_7AnyView_: 0x7f85d844bf90>. I don't know

CoreData: why not refresh UI when book.isPublic toggled

我与影子孤独终老i 提交于 2021-02-05 07:48:26
问题 '@Environment(.managedObjectContext) var context' will monitor the changes of context. When I click the button to toggle isPublic, I think it will cause to UI refreshing, Button text will change, But not, WHY? struct BookInfoView: View { @Environment(\.managedObjectContext) var context var isPublic: Bool { return book.isPublic } let book: Book var body: some View { print("refresh"); return Group { Button(action: onPublicToggled) { Text(isPublic ? "private" : "public") } } } func

CoreData: why not refresh UI when book.isPublic toggled

為{幸葍}努か 提交于 2021-02-05 07:46:36
问题 '@Environment(.managedObjectContext) var context' will monitor the changes of context. When I click the button to toggle isPublic, I think it will cause to UI refreshing, Button text will change, But not, WHY? struct BookInfoView: View { @Environment(\.managedObjectContext) var context var isPublic: Bool { return book.isPublic } let book: Book var body: some View { print("refresh"); return Group { Button(action: onPublicToggled) { Text(isPublic ? "private" : "public") } } } func

How to set a custom environment key in SwiftUI?

泪湿孤枕 提交于 2021-02-05 07:11:26
问题 Creating a custom environment key works, but if I wish to set the value in a view, XCODE doesn't allow it. But the predefined environment values can be set. What am I doing wrong? struct ResetDefault: EnvironmentKey { static var defaultValue: Bool = false } extension EnvironmentValues { var resetDefault: Bool { get { self[ResetDefault.self] } set { self[ResetDefault.self] = newValue } } } struct ResetView: View { @Environment(\.resetDefault) var reset var body: some View { Text("Reset")

Change Tabview color based on which tab is selected -Swiftui

夙愿已清 提交于 2021-02-05 07:10:03
问题 I am trying to see if I can make the color of the bottom tabview change depending on which tab item is selected. Currently I can make the tabview bar clear with the below code in the init. let tabBar = UITabBar.appearance() init() { tabBar.barTintColor = UIColor.clear tabBar.backgroundImage = UIImage() tabBar.shadowImage = UIImage() } ... TabView(selection: $selectedTab) { FirstView() .tabItem{ Text("First") } SecondView() .tabItem{ Text("Second") } } .onAppear{ setTabViewBackground() } func

Change Tabview color based on which tab is selected -Swiftui

喜欢而已 提交于 2021-02-05 07:09:39
问题 I am trying to see if I can make the color of the bottom tabview change depending on which tab item is selected. Currently I can make the tabview bar clear with the below code in the init. let tabBar = UITabBar.appearance() init() { tabBar.barTintColor = UIColor.clear tabBar.backgroundImage = UIImage() tabBar.shadowImage = UIImage() } ... TabView(selection: $selectedTab) { FirstView() .tabItem{ Text("First") } SecondView() .tabItem{ Text("Second") } } .onAppear{ setTabViewBackground() } func