swiftui

SwiftUI iOS app crashes when state change causes keyboard to resign as first responder while in a sheet

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-21 07:06:52
问题 I'm trying to create a view in a sheet with multiple "pages". The view holds a @State variable of an enum and I "switch" between pages with if self.page == .text {...} else if self.page == .image {...} else {...} However, when self.page is set while a textfield is active in one of the pages, the app crashes with no clear error message. This seems to work on the simulator but not on a device. Here's the dump: Thread 1 Queue : com.apple.main-thread (serial) #0 0x00000001f6dc702c in

Displaying an empty view in SwiftUI

十年热恋 提交于 2021-01-21 07:02:59
问题 In SwiftUI there's frequently a need to display an "empty" view based on some condition, e.g.: struct OptionalText: View { let text: String? var body: some View { guard let text = text else { return } return Text(text) } } Unfortunately, this doesn't compile since the body of guard has to return some view, that is an "empty" view when text is nil . How should this example be rewritten so that it compiles and renders an "empty" view when text is nil ? 回答1: You have to return something. If

Repeating animation on SwiftUI Image

穿精又带淫゛_ 提交于 2021-01-21 06:52:48
问题 Given the following struct : struct PeopleList : View { @State var angle: Double = 0.0 @State var isAnimating = true var foreverAnimation: Animation { Animation.linear(duration: 2.0) .repeatForever() } var body: some View { Button(action: {}, label: { Image(systemName: "arrow.2.circlepath") .rotationEffect(Angle(degrees: self.isAnimating ? self.angle : 0.0)) .onAppear { withAnimation(self.foreverAnimation) { self.angle += 10.0 } } }) } } I was hoping that the Image would rotate clockwise and

how to use .svg images in SwiftUI

99封情书 提交于 2021-01-21 05:18:50
问题 Can I only upload .png files into the assets folder? I get an error for .svg files. I want to use it for the Image() component 回答1: You can use Macaw to display a SVG: https://github.com/exyte/Macaw 1) Add the Package via SPM to your project (the tag 0.9.5 actually doesn't work, you need to use the master branch) 2) Move your .svg-file to the project or create a new Data set in your Assets.xcassets and then add the svg file to this data set. 3) Use this code: struct MyView: View { var svgName

How to return a Button from a function in SwiftUI?

◇◆丶佛笑我妖孽 提交于 2021-01-21 04:42:27
问题 I need to dynamically create a Button based on some parameters func buildButton(parameter : Parameter) -> Button { switch (parameter){ case Parameter.Value1: return Button( action: { ... }, label: { ... } ) case Parameter.Value2: return Button( action: {...}, label: { ... } ) } } But the compiler gives me this error: Reference to generic type 'Button' requires arguments in <...>. Insert '<<#Label: View#>>' So if I click Fix , the function declaration becomes func buildButton(parameter :

How to return a Button from a function in SwiftUI?

喜你入骨 提交于 2021-01-21 04:42:05
问题 I need to dynamically create a Button based on some parameters func buildButton(parameter : Parameter) -> Button { switch (parameter){ case Parameter.Value1: return Button( action: { ... }, label: { ... } ) case Parameter.Value2: return Button( action: {...}, label: { ... } ) } } But the compiler gives me this error: Reference to generic type 'Button' requires arguments in <...>. Insert '<<#Label: View#>>' So if I click Fix , the function declaration becomes func buildButton(parameter :

UserDefaults Binding with Toggle in SwiftUI

梦想的初衷 提交于 2021-01-21 01:33:14
问题 I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults . Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of truth) the Toggle to always show the value of the UserDefault I have watched many of the SwiftUI WWDC sessions, but I'm still not sure exactly how I should set everything up with the different tools that are available within Combine and SwiftUI. My

UserDefaults Binding with Toggle in SwiftUI

只谈情不闲聊 提交于 2021-01-21 01:32:12
问题 I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults . Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of truth) the Toggle to always show the value of the UserDefault I have watched many of the SwiftUI WWDC sessions, but I'm still not sure exactly how I should set everything up with the different tools that are available within Combine and SwiftUI. My

UserDefaults Binding with Toggle in SwiftUI

我是研究僧i 提交于 2021-01-21 01:30:29
问题 I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults . Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of truth) the Toggle to always show the value of the UserDefault I have watched many of the SwiftUI WWDC sessions, but I'm still not sure exactly how I should set everything up with the different tools that are available within Combine and SwiftUI. My

UserDefaults Binding with Toggle in SwiftUI

天涯浪子 提交于 2021-01-21 01:30:03
问题 I'm trying to figure out the best way to build a simple settings screen bound to UserDefaults . Basically, I have a Toggle and I want: the value a UserDefault to be saved any time this Toggle is changed (the UserDefault should be the source of truth) the Toggle to always show the value of the UserDefault I have watched many of the SwiftUI WWDC sessions, but I'm still not sure exactly how I should set everything up with the different tools that are available within Combine and SwiftUI. My