Questions tagged [swiftui-environment]

141 questions
3
votes
1 answer

How to create a UISearchBar in SwiftUI

I am trying to create my own SwiftUI version of a UISearchBar, for learning purposes. I have followed this tutorial and at this point my search bar struct is like this: import Foundation import SwiftUI struct SearchBarUI: View { @Binding…
Duck
  • 34,902
  • 47
  • 248
  • 470
3
votes
0 answers

What is the correct way to access SwiftUI @AppStorage and @SceneStorage values in helper functions?

As the @AppStorage and @SceneStorage property wrappers are only available in Views, this means I cannot specify them in my helper functions. For instance a helper function convertFromKelvin(degreesK: Double) -> Double may want to look up whether to…
zkarj
  • 657
  • 9
  • 23
3
votes
0 answers

SwiftUI NavigationView with PresentationMode creates bug in multilevel navigation hierarchy

I have an iOS 13.5 SwiftUI (macOS 10.15.6) app that requires the user to navigate two levels deep in a NavigationView hierarchy to play a game. The game is timed. I'd like to use custom back buttons in both levels, but if I do, the timer in the…
Gabriel Perdue
  • 1,553
  • 2
  • 15
  • 23
3
votes
1 answer

Update TabView and move to Home View after login

I have the fllowing TabViews in the ContentView. For the onAppear, it works fine. However, I am trying also to reload the tabs and move to the home after the user login from LoginView. Following is the code of the ContentView // …
Abdullah
  • 624
  • 6
  • 15
3
votes
0 answers

SwiftUI precondition failure: attribute failed to set an initial

I have two pages in SwiftUI which can be switched using tabView, one is ProjectPage the other is HabitPage. In the ProjectPage, a list of projects can be displayed. In the HabitPage, a list of habits can be displayed. New Habit can be added in this…
buibuibui404
  • 87
  • 1
  • 8
2
votes
1 answer

SwiftUI environment value not passed to child views when root is used from UIHostingController

I'm trying to force a layout direction using .environment(\.layoutDirection, .rightToLeft) modifier on a View. I'm using the following hierarchy: let layoutDirection = LayoutDirection.rightToLeft let view =…
2
votes
1 answer

How do I persist the button color after favoriting in SwiftUI?

I would like my favorites button to switch colors based on if the user favorites or unfavorites a character. I am able to change the text from "Favorite" to "Favorited" and change the color initially. If I exit the app, the button color goes back to…
Kevin
  • 27
  • 7
2
votes
2 answers

How to determine if a popover will be displayed as a popover or sheet in SwiftUI?

In SwiftUI when a popover is displayed, it will display as either a popover or sheet depending on the device (iPad or iPhone) and window space available. Is there a correct heuristic to check if the popover will be displayed as a popover or a…
pinglock
  • 982
  • 2
  • 12
  • 30
2
votes
2 answers

SwiftUI NavigationView pops back when updating observableObject

Background: I have been stuck on this annoying problem for days, trying different solution and searching stackoverflow etc for answers; some have had similar problems but no suggested solution had the desired effect. The problem is that when…
2
votes
3 answers

AutoComplete search in swiftui not working

I have a TextField as search box.I want to make it autosearch i.e whenever i will enter a letter in the TextField, i should get the result without press a enter.Here is my code: TextField("Search", text: $searchText, onEditingChanged: { …
2
votes
1 answer

SwiftUI -> Thread 1: Fatal error: No ObservableObject of type ModelData found

I created an app with SwiftUI and when I try to display a button this error message appears: Thread 1: Fatal error: No ObservableObject of type ModelData found. A View.environmentObject(_:) for ModelData may be missing as an ancestor of this…
2
votes
2 answers

Change Sign In With Apple button style based on current color scheme

I'm having some issues with SwiftUI's SignInWithAppleButton's signInWithAppleButtonStyle. I am trying to change the color of the button based on the user's current scheme or if it changes. This is iOS 14 and SwiftUI: @Environment(\.colorScheme) var…
Eric Agredo
  • 487
  • 1
  • 4
  • 13
2
votes
1 answer

Is there a way to override a view modifier?

I have a custom TextField and want to be able to use the .keyboardType(.numberPad) modifier on the CustomTextInputField rather than having to send it through an initializer. Is this possible to override the modifier or maybe get access to the…
Felipe
  • 498
  • 10
  • 29
2
votes
0 answers

SwiftUI - Location in API call with CLLocationManager and CLGeocoder

I'm struggling with this for a long time without finding where I'm wrong (I know I'm wrong). I have one API call with the location of the phone (this one is working), but I want the same API call with a manual location entered by a textfield (using…
2
votes
2 answers

Rectangle progress bar swiftUI

Hey does someone know how can I create rectangle progress bar in swiftUI? Something like this? https://i.stack.imgur.com/CMwB3.gif I have tried this: struct ProgressBar: View { @State var degress = 0.0 @Binding var shouldLoad: Bool var…
1
2
3
9 10