Provide views, controls, and layout structures for declaring your app's user interface using SwiftUI.

Posts under SwiftUI tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

objc[6125]: Class _PathPoint is implemented in both ...
I'm getting the following message every time I run the app: objc[6125]: Class _PathPoint is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore (0x11ef42e30) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/TextInputUI.framework/TextInputUI (0x16310a8b8). One of the two will be used. Which one is undefined. objc[6481]: Class RSABSSATokenBlinder is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/CryptoKitCBridging.framework/CryptoKitCBridging (0x1314c7470) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/CryptoKitPrivate.framework/CryptoKitPrivate (0x14f4c2368). One of the two will be used. Which one is undefined. I have no idea what that TextInputUI is or how it got there or why it's being repeated. How can I fix this without breaking its functionality?
15
4
6.8k
Aug ’23
SwiftUI context menu crash on iOS 14.x
I'm getting following crash while opening a context menu in LazyHStack item inside of LazyVStack: "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIPreviewTarget requires that the container view is in a window, but it is not. (container: <SwiftUI.HostingScrollView: 0x106876400>)'". Anyone can help me out how to avoid this crash issue ?
3
1
1k
Jan ’24
How To Show A SwiftUI Onboarding Screen Only When To App Launches For The First Time
I want to use onboarding screen in my project, and it is work but I want to use it just once time for app, I do not know how I will do it, is there any way? struct ContentView: View {   @State private var onboardinDone = false   var data = OnboardingData.data       var body: some View {     Group {       if !onboardinDone {         OnboardingView(data: data, doneFunction: {                              print("done onboarding")         })       } else {         MainScreen()       }     }   }        } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
2
1
1.6k
Sep ’23
SwiftUI List Separator within NavigationView Problem
I'm note sure if this is well-known-issue or not but it is very odd. The problem can be reproduced with Apple's example code navigationBarItems(leading:trailing:) As you can see, list separators has extra leading space that looks like they are indented for some reason. I test above code with Playground, iPhone 13/15.3.1 they are the same. I did messing around the code and found that applying .navigationBarTitle(), .navigationBarItems() to List causes the problem. They must apply to each List item. Very odd though. This means almost all List sample code that wrapping with NavigationView are WRONG. Here is a fix I found. Although I'm not sure if I can call this is a bug but definitely either document or implementation is wrong. Could anyone explain this?
6
0
3.4k
Feb ’24
SwiftUI Table Limit of Columns?
I am trying to build a Table using SwiftUI that has 14 columns. If I enter that many TableColumns under Table, I get the "The compiler is unable to type-check..." error. In fact, I get that error until I cut it back to 10 columns. My Columns all look something like this, with different name and value paths, of course:             TableColumn("Loc", value: \.location) I have seen that the way around this error is to break up the expression into something simpler. But I can't figure out how to simplify the number of columns. Any ideas? Thanks, Don
4
1
1.5k
Nov ’23
Sheet dismiss gesture with swipe back gesture causes app to freeze
I found a strange and frustrating behavior in SwiftUI. If you present a sheet from within a view in a navigation link and then (quickly and in order): swipe down to dismiss the sheet swipe back (from left edge to the right) to dismiss the navigation view The app will freeze before going back to the root view. Backgrounding the app and foregrounding it will unstick it. You have to perform the steps very quickly. Is there a workaround? I would like both gestures to work without interfering with each other. This code will reproduce the issue: struct ContentView: View { @State var showSheet = false var body: some View { NavigationView { VStack { NavigationLink("blah") { VStack { Button("Show Sheet") { showSheet.toggle() } .sheet(isPresented: $showSheet, onDismiss: nil) { Text("Sheet") } } } } } } } There is a gif linked in the Stack Overflow question demonstrating the problem.
4
3
1.9k
Sep ’23
Fixing a CommandMenu causing UIMenuController - unrecognized selector
I have a SwiftUI app I have enabled to run on M1 Macs (Designed for iPad). It uses the new SwiftUI lifecycle. I've just added a CommandMenu to the main WindowGroup. On the iPad these commands, whether clicked in the menu or initiated by a keyboard shortcut work as expected on the iPad. However, my iMac, the app crashes. The error says Application Specific Information: Crashing on exception: -[UIMenuController propertyList]: unrecognized selector sent to instance 0x12250b1e0 This is what the CommandMenu looks like WindowGroup { ... }.commands {         CommandMenu(LocalizedStringKey("Categories")) {             Button(action:{                 appConfiguration.currentPage = Page.books             }) {                 Label(LocalizedStringKey(BookListing.title), systemImage: BookListing.icon)             }             .keyboardShortcut("b", modifiers: [.control])             Button(action:{                 appConfiguration.currentPage = Page.tags             }) {                 Label(LocalizedStringKey(Tags.title), systemImage: Tags.icon)             }             .keyboardShortcut("t", modifiers: [.control])             Button(action:{                 appConfiguration.currentPage = Page.settings             }) {                 Label(LocalizedStringKey(Settings.title), systemImage: Settings.icon)             }             .keyboardShortcut("s", modifiers: [.control])         }     } AppConfiguration is an ObservableObject and setting currentPage is how to programmatically navigate the TavView. Is there anything I am doing wrong? Is there a bug someone knows about that I could be hitting? Here's the relevant part of the stack if that can help shed some light on what's gone wrong. Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 AppKit 0x191895514 -[NSApplication _crashOnException:] + 324 1 AppKit 0x19189550c -[NSApplication _crashOnException:] + 316 2 AppKit 0x19189522c -[NSApplication reportException:] + 580 3 AppKit 0x19193f708 uncaughtErrorProc + 156 4 HIToolbox 0x1975b10bc DispatchEventToHandlers(EventTargetRec*, OpaqueEventRef*, HandlerCallRec*) + 1696 5 HIToolbox 0x1975b02dc SendEventToEventTargetInternal(OpaqueEventRef*, OpaqueEventTargetRef*, HandlerCallRec*) + 356 6 HIToolbox 0x1975c6750 SendEventToEventTarget + 40 7 HIToolbox 0x197627310 SendHICommandEvent(unsigned int, HICommand const*, unsigned int, unsigned int, unsigned char, void const*, OpaqueEventTargetRef*, OpaqueEventTargetRef*, OpaqueEventRef**) + 416 8 HIToolbox 0x19764c44c SendMenuCommandWithContextAndModifiers + 56 9 HIToolbox 0x19764c3dc SendMenuItemSelectedEvent + 352 10 HIToolbox 0x19764c208 FinishMenuSelection(SelectionData*, MenuResult*, MenuResult*) + 100 11 HIToolbox 0x19764cbc8 MenuSelectCore(MenuData*, Point, double, unsigned int, OpaqueMenuRef**, unsigned short*) + 560 12 HIToolbox 0x19764c8e8 _HandleMenuSelection2 + 416 13 AppKit 0x1916b4ce4 _NSHandleCarbonMenuEvent + 300 14 AppKit 0x1916b4ac4 _DPSEventHandledByCarbon + 68 15 AppKit 0x191516bc4 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3280 16 AppKit 0x1915085f0 -[NSApplication run] + 596 17 AppKit 0x1914d9d08 NSApplicationMain + 1132 18 AppKit 0x1917afdd0 _NSApplicationMainWithInfoDictionary + 24 19 UIKitMacHelper 0x1a423aa7c UINSApplicationMain + 1276 20 UIKitCore 0x1b68d59f8 UIApplicationMain + 164 21 SwiftUI 0x1cc162374 closure #1 in KitRendererCommon(_:) + 164 22 SwiftUI 0x1cc1622cc runApp<A>(_:) + 252 23 SwiftUI 0x1cba55560 static App.main() + 128
3
0
1.8k
Oct ’23
What is "focus"?
In iOS 15 SDK you added the new FocusState API in SwiftUI. However there is no discussion or explanation anywhere that I could find, which explains: What exactly is "focus"? What isn't focus? What is the relationship between FocusState and accessibility focus? What is the relationship between whether a SecureField is being edited, and whether it's "focused"? Example: Lets say my tvOS app has an on-screen keyboard, where the user uses the remote's directional controls to move focus around to the letter buttons. To enter their password, they focus the password field, then click the center button to activate it. Now that it's active, they move focus to each letter of their password and click on each one: P... A... S... S... W... R... D... !... then they move focus to the "Submit" button and click. In this case, while the SecureField is being edited, focus moves around to a bunch of different buttons. The point of this example is that, if SecureField had a public "isBeingEdited" property, then it would be TRUE even while the field is not focused. However most Workday's designers interpret "focused" as being totally equivalent to "isBeingEdited" because in a web browser, tabbing out of a field makes it stop being edited. What is Apple's intent here? When not using a remote or physical keyboard or screen-reader, how is focus supposed to relate to whether a field is being edited? Does this relationship change when a user now has a bluetooth keyboard connected and Full Keyboard Access is turned ON? How does this correlate with accessibility focus? I cannot find any documentation from Apple that explains what focus is, or how this is supposed to work in SwiftUI in the various different scenarios where the concept of "focus" is relevant. Do you have a link to something current that explains how it's supposed to work so that we will know if there's a bug? Last question: how can we make the iOS simulator treat the physical keyboard as if it was a bluetooth keyboard to be used for focus-based keyboard navigation?
5
0
1.7k
Mar ’24
SwiftUI Table with TextField becomes unresponsive as entries increase
I am trying to implement a simple Table with a TextField that makes it possible to edit the data. Below is a very simple example where I am trying to do this. It works, but it becomes very unresponsive as the number of people increases. The example as it is, has 1,000 people. It is very slow when a TextField gets focus and is used to edit something. Strangely, it also gets very slow when you scroll through an entry that has been edited. It crashes completely when 10,000 people is used. I found this similar question on the web, but it is not answered. What should I do to implement an editable Table like this? struct ContentView: View { struct Person: Identifiable { var givenName: String var familyName: String let id = UUID() } @State private var people = [Person].init(repeating: Person(givenName: "Name", familyName: "Family"), count: 1000) var body: some View { Table($people) { TableColumn("Given Name") { $person in TextField("Name", text: $person.givenName) } TableColumn("Family Name") { $person in TextField("Family Name", text: $person.familyName) } TableColumn("Full Name") { $person in Text("\(person.givenName) \(person.familyName)")} } } }
1
3
1.6k
Oct ’23
SwiftUI Settings scene (Preferences): how call action when closed by user (click on red button or Cmd-W)
Hi, I thought this should be quite easy and maybe I only have tomatoes on my eyes, but I cannot find out how to call an action when the user clicks the red button or use CMD-W to close the Preferences window (= Settings Scene). I use Core Data. In the Preferences, many data structures, which define my system, are changed. I learned, that you shouldn't save too often to avoid performance problems, so now I want to save the Core Data context when the user closes the Preferences window. I tried .onDisappear, onChange of focus etc. but this didn't work. How can I define an action? Any hints are welcome :-)
2
0
1.1k
Apr ’24
@SceneStorage not working on macOS
Hi, using the following ContentView in a SwiftUI app on macOS I would expect that the state of the toggle persists across application launches: struct ContentView: View { @SceneStorage("Toggle") var onOrOff: Bool = false var body: some View { VStack { Toggle("Will it persist?", isOn: $onOrOff) .padding() } } } To my surprise it does not persist. Am I wrong about how @SceneStorage should work? (I am trying this on the lates macOS/Xcode versions) Does @SceneStorage work for anybody on macOS? Thanks for your feedback! Cheers, Michael
4
0
2.1k
Sep ’23
ScrollView and prefersDefaultFocus currently incompatible?
Hello, I just wanted to confirm that ScrollView and prefersDefaultFocus are currently not compatible, at least when building for tvOS A simple example:     HStack {       ScrollViewReader { proxy in         ScrollView(.horizontal, showsIndicators: true) {           VStack {             Button(action: {               resetFocus(in: namespace)             }) {               Text("RESET FOCUS")             }             HStack {               ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], id:  \.self) { v in                 Button(action: {}) {                   Text("ABC \(v)")                 }.prefersDefaultFocus(v == 11, in: namespace)               }             }           }.onAppear {             resetFocus(in: namespace)           }                     }       }       .focusScope(namespace)     }   } On load of the view the focus remains on the first button in the HStack. On pressing the RESET FOCUS button focus is applied to the first button visible on the left side of the screen. Am I missing something here? Is this the way these pieces of SwiftUI currently interact? e.g. prefersDefaultFocus has no effect inside a ScrollView? Is there a way to focus a specific item in a ScrollView programmatically? Thanks.
3
0
1.4k
Feb ’24
Quick Look Preview Extension
I'm trying to add a quick look preview extension to my app. But I can't find any reference on this On a earlier thread (https://developer.apple.com/forums/thread/704155) everyone seems to misunderstand my question. I MEAN THE QUICK LOOK YOU CAN TOGGLE IN FINDER THAT WILL SHOW YOU A PREVIEW OF A DOCUEMNT I don't want to add the tag QuickLook because that's not I want to do.
1
0
401
Feb ’24
SwiftUI List selection bug
Is there a way to optimize a List in SwiftUI? There is a problem with selection not working properly when the List has many rows. If you scroll to multi-select and up, some rows in the middle are not selected. I have an issue where selecting an unselected row deselects nearby rows. Is there any way for selection to work reliably even for a List of many rows? I would like to find a solution that is stable even when multiple lines are selected, like mail and note, which are the default apps in ios. ps. I am using CoreData. @State var selectedItem = Set<MyEntity>() List(selection: $selectItem){   ForEach(items, id: \.self){ item in     ContactsRow(contactsData: item)   } } .environment(\.editMode, $editMode)
1
0
1.3k
4w
Drag and Drop using SwiftUI
Overview I am bit confused regarding drag and drop on SwiftUI I think there are 2 approaches but I am stuck with both approaches WWDC22 When using the new draggable, dropDestination, Transferable API, only single items are draggable. Multiple items in a list are not draggable. I have filed a feedback FB10128110 WWDC21 I have faced a couple of issues for drag and drop introduced in WWDC21 (onDrag, onDrop, itemIdentifier), the Feedback ids are FB9854301, FB9854569, FB9855245, FB9855532, FB9855567, FB9855575. It contains sample projects, would really appreciate if someone could have a look it. Note: All feedbacks include a sample project with detail steps and some even have screenshots and videos Questions: If my approach is wrong or if I am missing something? Unfortunately I didn't manage to get a SwiftUI lab session (got cancelled), so please help me with these issues.
4
1
2.7k
4d
UIButton image disappears when “debug executable” is unchecked in edit scheme
I have created a UIButton in storyboard, and have set an image for the UIButton (the image is in assets.xcassets). The button shows up in the simulator and when run on an iPhone. However, if I quit the app on the iPhone and open it again, the button disappears, but I am still able to click it and it works. The same thing happens when I uncheck "debug executable." When I check the view hierarchy, the button is on top of the hierarchy but is invisible. Using infoButton.isHidden = false also does not work. In my button's view controller, I have called view.bringSubviewToFront(_: infoButton) in viewDidLoad(). I have also tried setting the button's image programmatically, but it still doesn't show up. However, when I set a background color for the button with infoButton.backgroundColor = UIColor.blue, I can see a blue box where the button should be, but no image. Here are my settings for the UIButton: I would appreciate any help, thank you.
2
1
1.5k
Oct ’23
Problem Running multiple Async tasks in View
Hey there, I have a problem running multiply tasks in parallel in a SwiftUI view. struct ModelsView: View { @StateObject var tasks = TasksViewModel() var body: some View { NavigationView{ ScrollView { ForEach(Array(zip(tasks.tasks.indices, tasks.tasks)), id: \.0) { task in NavigationLink(destination: ModelView()) { ModelPreviewView(model_name: "3dobject.usdz") .onAppear { if task.0 == tasks.tasks.count - 2 { Task { print(tasks.tasks.count) await tasks.fetch_tasks(count: 4) } } } } } }.navigationTitle("3D modelle") }.onAppear{ Task { await tasks.fetch_tasks(count: 5) await tasks.watch_for_new_tasks() } } } } In my view, I spawn a task as soon as the View Appears which, first, fetches 5 tasks from the database (this works fine), and then it starts watching for new tasks. In the Scroll View, right before the bottom is reached, I start loading new tasks. The problem is, the asynchronous function fetch_tasks(count: 4) only gets continued if the asynchronous function watch_for_new_tasks() stops blocking. actor TasksViewModel: ObservableObject { @MainActor @Published private(set) var tasks : [Tasks.Task] = [] private var last_fetched_id : String? = nil func fetch_tasks(count: UInt32) async { do { let tasks_data = try await RedisClient.shared.xrevrange(streamName: "tasks", end: last_fetched_id ?? "+" , start: "-", count: count) last_fetched_id = tasks_data.last?.id let fetched_tasks = tasks_data.compactMap { Tasks.Task(from: $0.data) } await MainActor.run { withAnimation(.easeInOut) { self.tasks.append(contentsOf: fetched_tasks) } } } catch { print("Error fetching taskss \(error)") } } func watch_for_new_tasks() async { while !Task.isCancelled { do { let tasks_data = try await RedisClient.shared.xread(streams: "tasks", ids: "$") let new_tasks = tasks_data.compactMap { Tasks.Task(from: $0.data) } await MainActor.run { for new_task in new_tasks.reversed() { withAnimation { self.tasks.insert(new_task, at: 0) } } } } catch { print(error) } } } ... } The asynchronous function watch_for_new_tasks() uses RedisClient.shared.xread(streams: "tasks", ids: "$") which blocks until at least one tasks is added to the Redis Stream. I tried running the watch_for_new_tasks() on a Task.detached tasks, but that also blocks. To be honest, I have no idea why this blocks, and I could use your guy's help if you could. Thank you in Advance, Michael
1
0
1.3k
Oct ’23