swiftui

Check state of Option-Key in SwiftUI (macOS)

放肆的年华 提交于 2021-02-10 12:39:59
问题 I'm looking for a way to check the state of the option-key in SwiftUI on macOS. I.e. depending on whether the option key is pressed or not I want to perform different actions in the .onTapGesture closure. 回答1: macOS-only SwiftUI has .modifiers modifier to specify EventModifiers , so your case is covered like in below example: Rectangle() .fill(Color.yellow) .frame(width: 100, height: 40) .gesture(TapGesture().modifiers(.option).onEnded { print("Do anyting on OPTION+CLICK") }) .onTapGesture {

Why is my child SwiftUI view not updating?

倖福魔咒の 提交于 2021-02-10 08:44:05
问题 I have a Swift ui view with a child witch gets values from a @State variable. When i update my @State variable the view rebuilds but the child stays the same. struct ContentView: View { @State var msg: String = "" var body: some View { VStack { Button(action: { self.msg = "Hallo World" }, label: { Text("Say Hallo")}) ChildView(msg: msg).padding().background(Color.orange) Text(msg).padding().background(Color.green) }.frame(maxWidth: .infinity, maxHeight: .infinity) } } struct ChildView: View {

iOS 13.4 Crashes app with vague message: precondition failure: type check failed: 101, expected Text, got _HiddenModifier

隐身守侯 提交于 2021-02-10 07:27:31
问题 Sorry to be vague in the description here, but this has caused my app to be completely DOA when a user attempts to use my search feature. This did not happen before iOS 13.4 and the error message is not helpful at all. Cant find any info. Has anyone run into this? where could it be? The app crashes at the point where in my SwiftUI file, I populate the view using a ForEach from an array, which is an observable object: ForEach(self.listArray.searchDataArray, id: \.self) { fetchedData in Ill

How to Use Bundle In SwiftUI with Image

寵の児 提交于 2021-02-10 07:24:14
问题 For example: i have Bundle in project , it is call "Game.Bundle" let b :Bundle = Bundle.init(path: Bundle.main.path(forResource:"Game", ofType:"bundle")!)! Image("Giyuu",bundle:self.b) but Bundle not work . how can i use custom Bundle 回答1: The SwiftUI Image(_ , bundle: _) looks for image resource in corresponding bundle's Assets catalog. In your case the image is just embedded as regular file, so you have to find and load it as file. Image itself cannot do that, so it should be constructed

How to Use Bundle In SwiftUI with Image

微笑、不失礼 提交于 2021-02-10 07:23:22
问题 For example: i have Bundle in project , it is call "Game.Bundle" let b :Bundle = Bundle.init(path: Bundle.main.path(forResource:"Game", ofType:"bundle")!)! Image("Giyuu",bundle:self.b) but Bundle not work . how can i use custom Bundle 回答1: The SwiftUI Image(_ , bundle: _) looks for image resource in corresponding bundle's Assets catalog. In your case the image is just embedded as regular file, so you have to find and load it as file. Image itself cannot do that, so it should be constructed

Hiding Navigation Bar in case of multiple Navigation Views in SwiftUI

前提是你 提交于 2021-02-10 07:09:46
问题 I am having trouble hiding the navigation bar in case of multiple navigation views. I want navigation bars to be present on first and second screen but not on the third one. struct FirstView: View { init() { UINavigationBar.appearance().backgroundColor = UIColor.green } var body: some View { NavigationView { NavigationLink(destination: SecondView()) { Text("Second View") }.navigationBarTitle("First View") } } } // Second View struct SecondView: View { var body: some View { NavigationLink

Update data in CoreData and its value in a view

故事扮演 提交于 2021-02-10 06:43:34
问题 I am new to developing in Swift and CoreData. I've been struggling with the following code which displays a view with some information on a student which is stored with CoreData. The button "Ask question" which is displayed in the view updates student.questionAskedClass but the view does not updates its value. The value is clearly updated internally because if I relaunch the application, I can see that the value has been updated. But I would like to see it updated live. Thanks for your help,

How to set default clouse param in View method?

六眼飞鱼酱① 提交于 2021-02-10 06:18:28
问题 I tried to set else default param in ifLet method but I face an error: Protocol 'View' can only be used as a generic constraint because it has Self or associated type requirements . What did wrong? extension View { func ifLet<Value, Then: View, Else: View>( _ value: Value?, then: (Value) -> Then, else: () -> View = { EmptyView() } ) -> _ConditionalContent<Then, Else> { if let value = value { return ViewBuilder.buildEither(first: then(value)) } else { return ViewBuilder.buildEither(second:

How to Create a SwiftUI RoundedStar Shape?

一笑奈何 提交于 2021-02-10 06:10:18
问题 This is a self-answered question which is perfectly acceptable (and even encouraged) on Stack Overflow. The point is to share something useful to others. SwiftUI has a RoundedRectangle Shape . It would be nice to have a five-pointed star with rounded tips that could be used for filling, clipping, and animation. This Stack Overflow answer shows how to make a RoundedStar as a custom UIView using UIBezierPath . How can this code be adapted to SwiftUI as a Shape that can be animated? 回答1: Here is

why is VStack not working in GeometryReader with scrollview?

旧时模样 提交于 2021-02-10 06:09:51
问题 My scrollview with vStack worked (in AppleTV - i did not test in iOS) without GeometryReader. Then i added the geometryreader and the VStack "collapses" like a ZStack. What can i do to solve this? (and yes, i need the geometryreader ;)) struct ContentView: View { @State var offset : CGFloat = 0 var body: some View { ScrollView(.vertical, showsIndicators: false) { VStack(alignment: .leading) { GeometryReader { geometry -> AnyView in let newOffset = geometry.frame(in: .global).minY if newOffset