Post

Replies

Boosts

Views

Activity

searchable: Add an auxilliary search view
Is there a clean way to add an auxilliary view to the SwiftUI .searchable view? The best I have found is to add a VStack above the list being searched, but this makes the removal transition behave funny. ("Funny" means the search view and aux views animate away fine, but after they finish animating the List view snaps to the top of the screen, hiding the idle search field. The normal behavior leaves the idle search field showing.) NavigationStack { MyListView().searchable(...) } ——— struct MyListView: View { @Environment(\.isSearching) private var isSearching var body: some View { if isSearching { VStack {...my auxilliary view...} } List {...} } }
3
0
246
Sep ’24
Using KeyFrameTimeline as value for .keyFrameAnimator view modifier
I have several views I want to animate in concert. I plan to use keyframe animation. I want to keep the .keyFrameAnimator modifier code small; I have a lot of ...Keyframes inside several KeyframeTracks. It seems like I should be able to isolate the body of the keyframes parameter into a func or var. Builders are such a pain, I can't grok the right way to refactor their bodies out. I've tried to make a standalone @KeyframeTrackContentBuilder<MyValue> function but cannot figure out the right syntax/incantation to stuff it with KeyframeTracks. My latest attempt is to create a func that returns a KeyframeTimeline, but that's been a deadend too. let k: KeyframeTimeline<MyValue> = timeline(...) CartoonCardView(color: .yellow) .keyframeAnimator( initialValue: k.value(time: 0) ) { content, value in content .rotationEffect(value.angle) .scaleEffect(value.scale) .offset(value.offset) } keyframes: { _ in k } The error on k in the last line is "No exact matches in call to static method 'buildExpression'" with the sub-error "Candidate requires that 'KeyframeTimeline' conform to 'KeyframeTrackContent' (requirement specified as 'K' : 'KeyframeTrackContent') (SwiftUICore.KeyframesBuilder)"
4
0
485
Jul ’24
tvOS play/pause gesture and non-av views
I have a two-view app where the main view is a procedural animation and a secondary view controls settings for the animation. I want to use Play/Pause to toggle between the views, but can't figure out how to do this. Ideally the main view does not have any visible control and the whole screen can be dedicated to the animation view. Attaching onPlayPauseCommand to the main view does not work. I've also tried managing focus using onFocus without success. I'm open to other ways to toggle between the main and settings views, it's just that Play/Pause seems the most intuitive.
1
0
558
Apr ’24
Unit test SwiftData migrations
I'm trying to write a unit test for a SwiftData migration. In the teardown function I delete the SQLite container files, but then the underlying sqlite library complains. There must be a way to gracefully terminate the SwiftData container before I delete the files, but I don't see how. Simplying nil-ifying the references doesn't work. I don't see any obvious close functions, so I hope someone knows a non-obvious function. override func tearDownWithError() throws { // Cleanup resources // -- DOES NOT CLOSE UNDERLYING SQLITE ACCESS -- self.container = nil self.context = nil // Delete database do { try FileManager.default.removeItem(at: self.url) } catch { // Ignore file not found, report everything else. let nserror = error as NSError if nserror.domain != "NSCocoaErrorDomain" && nserror.code == 4 { throw error } } try? FileManager.default.removeItem(at: self.url.deletingPathExtension().appendingPathExtension("store-shm")) try? FileManager.default.removeItem(at: self.url.deletingPathExtension().appendingPathExtension("store-wal")) } I get these errors for .store, store-shm, and .store-wal: BUG IN CLIENT OF libsqlite3.dylib: database integrity compromised by API violation: vnode unlinked while in use: /Users/(ME)/Library/Developer/XCTestDevices/C52F4E12-EB4F-4639-9866-C3A7126155FA/data/Containers/Data/Application/B0EE90C6-B95D-4185-890D-6F20766B9B3B/tmp/test1.store invalidated open fd: 11 (0x11) If the worst comes to the worst, I'll work around it by using a differently-named container for each test, but as they're in tmp they'll get cleaned up for me eventually.
1
0
717
Feb ’24
ShareLink FileRepresentation exports message as a file
When I save an item which is a FileRepresentation to the File system/Files app, TWO files are saved: the shared file, and simple text file containing the message text. I don't want the user to get the message in a text file when they save that way. Sure I can just leave out the message parameter, but then it's useful if they want to email the file somewhere? Is there a way to have a message text that isbn't saved in a file? ShareLink(item: ..., subject: Text("..."), message: Text("..."), //⬅ this text gets saved in a 2nd file preview: SharePreview("...", image: ...)) { Label { Text("...") } icon: { Image(systemName: "square.and.arrow.up") } }
2
0
626
Nov ’23
Animate list row height
How can I tell List to animate row height changes? I have a list whose rows may grow or shrink with user actions. When that happens the List is redrawn with the new heights instantly but the contents slide to their new position. It'd be better if the row heights were animated. Since I don't know what a row's height is I can't use the usual .frame(height: self.animate ? 60 : 90) This code demonstrates the problem. struct ContentView: View { @State private var numbers: [Int] = [0,1,2,3] var body: some View { VStack { List { Item(numbers: $numbers) Item(numbers: .constant([9,8,7])) } .font(.largeTitle) HStack { Button { withAnimation { addNumber() }} label: { Text("Add") } Spacer() Button { withAnimation { removeNumber() }} label: { Text("Remove") } } } .padding() } private func addNumber() { numbers.append(numbers.count) } private func removeNumber() { numbers = numbers.dropLast() } } struct Item: View { @Binding var numbers: [Int] var body: some View { VStack { ForEach(numbers, id:\.self) { n in Text("\(n)") } } } }
0
2
755
Oct ’23
Unexpected intents
I haven’t done any work for Intents so I don’t know why iOS is making Siri suggestions for my app. Every now and then, maybe especially in the morning, my iPhone will show a button at the bottom of the Lock Screen with my app icon, the title of a data record from inside my app, and the caption “Siri suggestion”. Tapping it launches my app but that’s it. The app doesn’t show the record. Why is iOS doing this? Is this some half-baked effect of using iCloud data or Swift Data? I can’t release the app with iOS doing this, and adding proper Intent support would delay the release.
1
0
592
Oct ’23
ScrollViewReader and alignmentGuide
ScrollViewReader doesn't seem to work reliably with an animated alignmentGuide view as the target. If I scroll by hand then SVR will track the animated guide. Rescaling the content often breaks the behavior and requires another manual scroll to reconnect the animation to scrolling. In my test I have a media player-like timeline. I'd like to keep the elapsed time cursor centered, no matter the length of the timeline rectangle. struct ContentView: View { @State private var zoom: Double = 10 let start = Date.now let duration: TimeInterval = 60 var body: some View { VStack(alignment: .leading) { TimelineView(.periodic(from: start, by: 1)) { context in let elapsed = context.date.timeIntervalSince(start) ScrollViewReader { proxy in ScrollView(.horizontal) { Text("\(elapsed.formatted(.number.precision(.fractionLength(0)))) / \(duration.formatted(.number.precision(.fractionLength(0))))") let barWidth: CGFloat = max(300, duration * zoom) ZStack(alignment: Alignment(horizontal: .marker, vertical: .bottom)) { ZStack(alignment: .leading) { // The bar representing the duration. Rectangle().fill(Color.gray) // The bar representing the elapsed time. Rectangle().fill(Color.accentColor) .frame(width: barWidth * (elapsed / duration)) .alignmentGuide(.marker) { d in d[.trailing] } } .frame(width: barWidth, height: 20) Rectangle().fill(Color.primary).frame(width: 3) .alignmentGuide(.marker) { d in d[HorizontalAlignment.trailing] } .id(1) } .frame(height: 30) .onChange(of: elapsed, perform: { _ in proxy.scrollTo(1, anchor: .center) }) } .onChange(of: elapsed, perform: { newValue in if newValue > duration { exit(0) } }) } } Slider(value: $zoom, in: 0.5...40, label: { Text("Zoom") }) let s = zoom.formatted(.number.precision(.fractionLength(1))) Text("\(s)") } .padding() } }
0
0
663
Feb ’23
Custom File "New Document" to show template-picker in a document-based app
I am writing a document-based app for macOS using SwiftUI. I want the File menu's New Document command to show a template picker/wizard, and then let the wizard create the document. How do I structure this? Is there documentation? Examples? I tried this pattern @main struct DocDemoApp: App { var body: some Scene { WindowGroup { NewDocWizard() }     DocumentGroup(newDocument: { DocDemoDocument() }) {         ContentView(document: $0.document)     } } } The NewDocWizard calls newDocument({ DocDemoDocument() }). But the WindowGroup makes a File > New Window command while DocumentGroup makes the File > New Document command. I need just New Document and it should show the NewDocWizard.
1
0
1.2k
Jan ’23
(Xcode 14.0 beta 5) ThreadSanitizer: CHECK failed: tsan_platform_posix.cpp:83 "((beg)) <= ((end))"
Similarly to this old post [https://developer.apple.com/forums/thread/130969] I am getting a "ThreadSanitizer: CHECK failed" before my code seems to have launched. Is this something I should spend time trying to reproduce in a small project or is it an Xcode bug others are getting too? Turning off the sanitizer is my dirty workaround for now.
2
0
2.6k
Aug ’22