swiftui

SwiftUI | Get current scroll position from ScrollView

ぃ、小莉子 提交于 2020-11-27 01:55:39
问题 With the new ScrolLViewReader , it seems possible to set the scroll offset programmatically. But I was wondering if it is also possible to get the current scroll position? It seems like the ScrollViewProxy only comes with the scrollTo method, allowing us to set the offset. Thanks! 回答1: It was possible to read it and before. Here is a solution based on view preferences. struct DemoScrollViewOffsetView: View { @State private var offset = CGFloat.zero var body: some View { ScrollView { VStack {

How do I set a group of toggles to be mutually exclusive in swift

青春壹個敷衍的年華 提交于 2020-11-25 03:58:34
问题 @State private var isOn1 = false @State private var isOn2 = false var body: some View { ScrollView{ Toggle("switch1",isOn:$isOn1) Toggle("switch2",isOn:$isOn2) } Here is what I have so far. Is there a way for me to add an if statement when activating a toggle that all the other toggles will be off. Or is there anyway to change the states of the toggles when activating one. Any answers are appreciated thank you! 回答1: You can define isOn2 as a Binding . You can create a Binding by passing in a

Clear background for form sections in SwiftUI?

雨燕双飞 提交于 2020-11-25 03:46:47
问题 I'm trying to remove the white background of some sections so the elements lay right on the grey background, but I can't get the section background to be removed or transparent. This is what I'm trying: struct ContentView: View { var body: some View { Form { Section { Text("Hello!") Button { print("Clicked") } label: { Text("Click Me!!") } } Section { VStack { Button("Button 1") {} Spacer() Button("Button 2") {} } } .background(Color.clear) // Not doing anything Section { VStack(alignment:

NavigationView pops back to root, omitting intermediate view

↘锁芯ラ 提交于 2020-11-25 03:46:45
问题 In my navigation, I want to be able to go from ContentView -> ModelListView -> ModelEditView OR ModelAddView . Got this working, my issue now being that when I hit the Back button from ModelAddView , the intermediate view is omitted and it pops back to ContentView ; a behaviour that ModelEditView does not have. There's a reason for that I guess – how can I get back to ModelListView when dismissing ModelAddView ? Here's the code: import SwiftUI struct ContentView: View { var body: some View {

NavigationView pops back to root, omitting intermediate view

匆匆过客 提交于 2020-11-25 03:46:39
问题 In my navigation, I want to be able to go from ContentView -> ModelListView -> ModelEditView OR ModelAddView . Got this working, my issue now being that when I hit the Back button from ModelAddView , the intermediate view is omitted and it pops back to ContentView ; a behaviour that ModelEditView does not have. There's a reason for that I guess – how can I get back to ModelListView when dismissing ModelAddView ? Here's the code: import SwiftUI struct ContentView: View { var body: some View {

NavigationView pops back to root, omitting intermediate view

筅森魡賤 提交于 2020-11-25 03:46:27
问题 In my navigation, I want to be able to go from ContentView -> ModelListView -> ModelEditView OR ModelAddView . Got this working, my issue now being that when I hit the Back button from ModelAddView , the intermediate view is omitted and it pops back to ContentView ; a behaviour that ModelEditView does not have. There's a reason for that I guess – how can I get back to ModelListView when dismissing ModelAddView ? Here's the code: import SwiftUI struct ContentView: View { var body: some View {

NavigationView pops back to root, omitting intermediate view

僤鯓⒐⒋嵵緔 提交于 2020-11-25 03:45:39
问题 In my navigation, I want to be able to go from ContentView -> ModelListView -> ModelEditView OR ModelAddView . Got this working, my issue now being that when I hit the Back button from ModelAddView , the intermediate view is omitted and it pops back to ContentView ; a behaviour that ModelEditView does not have. There's a reason for that I guess – how can I get back to ModelListView when dismissing ModelAddView ? Here's the code: import SwiftUI struct ContentView: View { var body: some View {

Clear background for form sections in SwiftUI?

筅森魡賤 提交于 2020-11-25 03:43:33
问题 I'm trying to remove the white background of some sections so the elements lay right on the grey background, but I can't get the section background to be removed or transparent. This is what I'm trying: struct ContentView: View { var body: some View { Form { Section { Text("Hello!") Button { print("Clicked") } label: { Text("Click Me!!") } } Section { VStack { Button("Button 1") {} Spacer() Button("Button 2") {} } } .background(Color.clear) // Not doing anything Section { VStack(alignment:

How do I correctly pass a “cell item” to a .sheet from a SwiftUI LazyVGrid?

浪子不回头ぞ 提交于 2020-11-25 03:40:33
问题 Here is my example, and I can't tell if this is a bug or not. All my cells load correctly, but when I try to bring up the DetailView() as a sheet, the item pased in is always whatevr item is shown first in the grid (in the top left in my case here), NOT the "cell" that was tapped. So, why is the item from ForEach loop correctly populating the cells, but not being passed to the .sheet via the button? import SwiftUI let columnCount: Int = 11 let gridSpacing: CGFloat = 1 struct GridView: View {

SwiftUI: What is @AppStorage property wrapper

吃可爱长大的小学妹 提交于 2020-11-24 17:33:47
问题 I used to save important App data like login credentials into UserDefaults using the following statement: UserDefaults.standard.set("sample@email.com", forKey: "emailAddress") Now, I have come to know SwiftUI has introduced new property wrapper called: @AppStorage Could anyone please explain how the new feature works? 回答1: @AppStorage is a convenient way to save and read variables from UserDefaults and use them in the same way as @State properties. It can be seen as a @State property which is