Post

Replies

Boosts

Views

Created

Positioning views: is it me or SwiftUI ?
After more than 2 years of intense use of SwiftUI I appreciate many of its features, particularly the speed at which I can get some code running. But, there are some major buts … Screen Position, I keep struggling with try, test, error and retry to position views relative to each others, withy many iterations to tune the x and y parameters. And preview does not help when view is too complex: I find it more reliable to test in simulator. At the end, my code is messed with .position, .offset, … that I manually adjusted but may break if I change something elsewhere in the body. The fact that position is relative to center of view (different from UIKit) makes it even more mind boggling. For sure, UIKit constraints were not so easy, but you got a very stable code. That's not the case with SwiftUI z position In some cases, I want the View to appear on front of all others. It may be a very simple Rectangle() with overlay, e.g., to mimic an alert. And I struggle with the Rectangle drawn behind another view, even trying to adjust the zIndex to no avail… Should I create an explicit View instead of creating the Rectangle directly in the body ? So am I misusing SwiftUI, or are they points that SwiftUI should improve in the future ?
1
1
901
Aug ’23
How to declare Privacy manifest
It is stated that From Fall 2023 you’ll receive an email from Apple if you upload an app to App Store Connect that uses required reason API without describing the reason in its privacy manifest file. From Spring 2024, apps that don’t describe their use of required reason API in their privacy manifest file won’t be accepted by App Store Connect. There are some answers here : https://developer.apple.com/videos/play/wwdc2023/10060/ but far from answering all questions. I have questions on how to implement: Where exactly is the privacy manifest ? How to create it, from which file template in Xcode ? WWDC speaks of a PrivacyInfo.xcprivacy (does it require a more recent version of Xcode than 14.2). WWDC describes a framework case. Is it the same for a "final" app ? is there a specific format for describing the reason ? Or just plain text. Is this text visible to the user or only to reviewer ? does it apply retroactively to apps already in AppStore (do they need to be resubmitted ?). It seems not. So I tried, in an iOS App, to declare the PrivacyInfo.xcprivacy as explained, with Xcode 14.2, using plist template, to no avail. Really not clear on how to proceed or even start… We would need a clear step by step tutorial with all prerequisites (Xcode or MacOS versions needed for instance).
14
1
18k
Jul ’23
Default Text width in SwiftUI different for MacOS and iPadOS ?
I have an app that runs eitheir as MacOS or iPadOS. After a few changes to adapt to API differences (PasteBoard notably), app runs OK on both. But the layout of views is messy when running on iPad. Reason seems to be that Text are not using the same default font size, or that the same system font size does not use the same space. Same issue when using segmented picker for instance. Configuration: Xcode 14.2 MacOS 12.6.6 Simulator: iPad Pro (12.9") - iOS 16.2 Is it a correct analysis ? Is there a simple workaround, to avoid the need to redesign all views for iPadOS ?
0
0
788
Jun ’23
Execution order of multiple onChange
I have declared an array as a State var @State var proba : [Int] = [10, 40, 40, 10] The array is updated in onAppear : .onAppear { proba = arrayOfTuples.map { (proba, _) in proba } print("proba", proba) } The array is used for TextFields: ForEach(0..<proba.count, id: \.self) { index in TextField("N", value: $proba[index], format: .number) .frame(width: 40) .onChange(of: proba_kWh[index], perform: {[oldValue = proba_kWh[index]] newValue in print("proba onChange", index, oldValue, proba[index], newValue) }) When View appears, I get some onChange (as initial values did change) proba [10, 20, 60, 10] // onAppear changes 2 values: 1 and 2 proba onChange 2 40 60 60 // index 2 is changed before 1 proba onChange 1 40 20 20 I know that order of execution is not guaranteed, but in this specific case, I did thought it would be in the order of the array reading…
0
0
735
May ’23
Dark mode switching modifies ObservedObject
This is a SwiftUI Mac App. There is an observable object with a published array class AllObjects : ObservableObject { @Published var theItems = [Item]() } In the View, observed with: @ObservedObject var allObjects : AllObjects Item class is : class Item: ObservableObject, Identifiable { let id = UUID() var placeNo = 1 // Other properties } When I switch dark / light mode within the app (with a toggle) @Environment(\.colorScheme) var colorScheme the ObservedObject allObjects is modified: array is emptied: allObjects.theItems is now an empty array. What could cause the change to theItems ? I have checked that the only func un which theItems is reset to [] is not called.
2
0
685
Apr ’23
Design pattern for scheduling simulation steps
I'm running a simulation (SwiftUI app), which has 100 steps. I need each step to be executed in order. A first try was to dispatch with delay to schedule each second: for step in 0..<100 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } Very poor results as 100 running threads are too much load for the system. So I split in 2 stages: for bigStep in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(bigStep) * 10.0 ) { for step in 0..<10 { DispatchQueue.main.asyncAfter(deadline: .now() + Double(step) * 1.0) { // simulation code } } } } It works much better, as now there are a max of 20 threads active (in fact I create more levels to limit to a max of 8 concurrent threads). It addition, it allows to interrupt the simulation before end. My questions: is it the appropriate pattern ? Would a timer be better ? Other options ?
4
0
1.2k
Apr ’23
On daylight saving time change
Last night we change hour for daylight saving time (at 2:00 it would be 3:00). So I made a simple test to set an alarm at 2:10 At 1:59, clock jumped logically to 3:00 no alarm At 3:10, alarm rang (in fact I had turned it off and back on, but that has no influence) I set an alarm at 3:15, of course it rang I set a new alarm at 2:20 It rang at 3:20 Conclusion: Clock app "replicates" the alarms between 2:00 and 2:59 into 3:00 - 3:59. Which is great, not to miss any. Question: Is this specific to Clock app or a more general system behaviour for all time events ? If so, there may be a side effect: I set an event A to turn On a light at 2:45 And set event B to turn it Off at 3:15 Then B will occur before A and the light will remain On There is no perfect solution ("warping" 2:00 - 2:59 into a minute at 3:00 would creates other issues). Extra question: What happens on winter time ? Will alarm ring twice at 2:10 ?
0
0
2.2k
Mar ’23
List issues with dynamic content
I have encountered the following problem with a List. The setup is as follows @State private var allItems : [SomeItem] @State private var selected   : SomeItem? // in the body List(allItems, $selection) { theItem in … } where SomeItem is a struct. When some properties of an item in allItems changes, the values that I read in theItem are not updated. Just as if old content was cached. I changed allItems to a computed var and everything works OK. I read this SO thread https://stackoverflow.com/questions/74083515/swiftui-list-item-not-updated-if-model-is-wrapped-in-state but that does not give a full explanation. So my questions: Is there effectively an issue here ? when is it safe to use a State var as the Content of List ? Is it OK (it seems) if the properties of items do not change ? It seems also OK if the list of allItems is modified (appended, reduced) without problem changing the properties of its elements. How to do if needed to change both allItems (append for instance) and change the properties of some items, as computed var cannot be modified. Hope the question is clear.
3
0
1.1k
Feb ’23
ViewBuilder does not force View update
I use a ViewBuilder to generate the destination used in a NavigationSplitView (to select the detail View). This viewBuilder depends on a parameter (in fact 2, but I try to simplify). ViewBuilder is simple, just calls a View: @ViewBuilder func destination(object: SomeObject, name: String) -> some View { MyView(objToUse: object, nameToUse: name) } But this does not work. When I change the selection in the Master of the splitView, view is not updated (even though I've checked the content is updated. This si so simple that I started using directly MyView(objToUse: object, nameToUse: name) in the detail View. It did not work either. Now, here is the surprise: if I use a switch statement in the ViewBuilder, it works: Let's say we have: struct SomeContent: Hashable, Identifiable, Codable { var id = UUID() var name: String } struct Object : Hashable, Identifiable, Equatable, Codable { var id = UUID() var content: [SomeContent] = [] } So I define a func to get all the names func allNames(of object: SomeObject) -> [String] { var names : [String] = [] for o in object.content { names.append(o.name) } return names } And modify ViewBuilder as follows: it works @ViewBuilder func destination(object: SomeObject, name: String) -> some View { let names : [String] = allNames(of: object) switch name { case names[0]: MyView(objToUse: object, nameToUse: name) case names[1]: MyView(objToUse: object, nameToUse: name) case names[2]: MyView(objToUse: object, nameToUse: name) default: EmptyView() } It also works with nested if else if instead of a switch. What is it I am missing ?
2
0
912
Jan ’23
Button behaviour changes if position(x:y)
With this code, button is at the center of the view and message is logged only when tapping in the button 1. struct ContentView: View { 2. var body: some View { 3. 4. ZStack { 5. Button(action: { 6. print("Touched") 7. }) { 8. Image(systemName: "square.split.diagonal.2x2") 9. .font(.system(size: 24)) 10. .foregroundColor(.black) 11. // .position(x: 12, y: 12) 12. } 13. 14. } 15. .frame(width: 300, height: 300) 16. .background(Color.yellow) 17. } 18. } But if I uncomment line 11, the message is logged on tap anywhere in the view. How is it position() changes the behaviour ?
3
0
1.1k
Dec ’22