Questions tagged [swiftui-state]

47 questions
1
vote
2 answers

Update SwiftUI Date Picker to match a model's computed property: Cannot assign to property

I have a form which lets the user create a project. In this form, I want to automatically change the project's deadline DatePicker based on the user's input such as start date, daily count, and target count. If the DatePicker's date is changed, then…
1
vote
1 answer

Create array of state objects in SwiftUI

In my project I have a 2D array of my custom class, GridTile representing a grid. This is the class: import Foundation import SwiftUI class GridTile: ObservableObject { @Published var tileNumber = 0 var tileFrame = CGRect.zero var…
JackElia
  • 140
  • 9
1
vote
1 answer

SwiftUI - view expand from the bottom of frame

I'd like to display a number of values as a continuous value from 0 to 1. I'd like them to grow from the bottom up, from 0 displaying no value, to 1 displaying a full height. However, I'm unable to make it "grow from the bottom". I'm not sure what a…
Hoopes
  • 3,943
  • 4
  • 44
  • 60
1
vote
1 answer

Multiple View transition animations not happening at once

Very early into SwiftUI programming, I am basically trying to show a list of cards inside a container view, with the top most one being shown... Each card has a text view and when tapped the card view is removed from the stack/container view and the…
Prasad
  • 166
  • 7
1
vote
1 answer

How to avoid SwiftUI Initial View getting initialized multiple times when @State variables are initialized?

struct GroceryList: View { @ObservedObject var coreDataViewModel:CoreDataViewModel = CoreDataViewModel() @StateObject var userPreferences: UserPreferences = UserPreferences() @State private var selectedItemsInList: [GroceryItem] = [] @State…
udGlobal
  • 17
  • 7
1
vote
0 answers

How do I pass a @State binding to the Content closure of a custom container?

I'm starting to play with custom containers and I'm stuck trying to send the @State var across. Sample code: struct ZoomCardView: View { let contentDefault: ((Binding)) -> Content let contentZoomed: (Binding) ->…
titusmagnus
  • 2,014
  • 3
  • 23
  • 23
1
vote
2 answers

Generating views from API call - SwiftUI

I'm new to SwiftUI, and was wondering if someone could help me make my app work. Also I'm trying to get my head around MVVM, and was wondering if I was doing it correctly. The app makes a call to https://jsonplaceholder.typicode.com/users and I'm…
1
vote
1 answer

SwiftUI State Variable not Rendering View Unless Used Inside View (verses in modifier)

Sorry for terrible wording, but in onAppear I am changing a State variable, however this does not re-render the view unless I use that State variable somewhere inside the view. I am trying to pop a modal if the user is not logged in, but it won't…
connorvo
  • 761
  • 2
  • 7
  • 21
1
vote
1 answer

SwiftUI State value doesn't change

I am not familiar with State. I tested the code: import SwiftUI class TestState { @State var a: Int = 3 func increaseA() { print("before \(a)") //3 a += 1 print("after \(a)") //3? } } TestState().increaseA() It…
蘇哲聖
  • 735
  • 9
  • 17
1
vote
1 answer

SwiftUI animate all except .foregroundColor

Based on the value of a @State var, how can I animate any other parameters but one? For instance, I want to animate .offset but not the .foregroundColor. It may sound like an easy task to do, but it's not so easy when using the same "base view" to…
Aнгел
  • 1,361
  • 3
  • 17
  • 32
1
vote
1 answer

SwiftUI - Change View variable based on input parameter

How do I change the @State variable size in a view based on the input parameter categorySize supplied when calling the view? I'd like to change the size to 140 when categorySize == .Large and to 40 when categorySize == .Small. enum ExampleEnum { …
Peanutsmasher
  • 220
  • 3
  • 13
0
votes
0 answers

SwiftUI changing State variable in code does not refresh list

I have a list of matches and when I type in the filter (TextField) it works fine. I also have a list of players the user can pick from. When they choose a player the func playerFilterChanged is called and usernameSearch is updated from the…
Brian Kalski
  • 897
  • 9
  • 35
0
votes
0 answers

SwiftUI Button animation

I use ButtonStyle, for button style. But when I updated button from enable to disable, and change thread, the button change without animation. //I have ButtonStyle: struct WizardButtonStyle: ButtonStyle { enum Style { case enabled, disabled } …
0
votes
1 answer

NavigationView doesn't update when @State changes (NavigationStack works well)

I encountered this situation when trying to replace my NavigationStack (iOS 16+) by a NavigationView (for backward compatibility reasons). What I hoped for would be that NavigationStack and NavigationView would have similar behavior since Swift UI…
0
votes
2 answers

SwiftUI State in Preview with PreviewProvider is not working

I've a simple button that work on simulator as expected, the shape changes with click; struct ButtonTest: View { @State var isLiked = true var body: some View { VStack { LikeButtonSimple( action: toggleLiked, isLiked:…
kelalaka
  • 5,064
  • 5
  • 27
  • 44