Post

Replies

Boosts

Views

Activity

iOS: List selection is reset to nil when app is sent to background
For some reason, a List will reset its selection to nil when the app is in the background. Steps to reproduce the issue: Run attached sample project Once the app has launched, select a name in the sidebar Move the app to the background Wait a few seconds Bring back the app to the foreground Expected result: The list selection should still be valid Actual result: The list selection is set to nil Notes: I’m using a StateObject, which should be the way to ensure that data isn’t regenerated when views are rendered. Is this a bug or something else needs to be taken care of? class AppModel: ObservableObject {     @Published var selectedPerson: Person? } @main struct NilListSelectionApp: App {     @StateObject var appModel = AppModel()     var body: some Scene {         WindowGroup {             ContentView()                 .environmentObject(appModel)         }     } } struct Person: Identifiable, Hashable {     let id: UUID     let firstname: String     init(firstname: String) {         id = UUID()         self.firstname = firstname     } } struct ContentView: View {     @EnvironmentObject private var appModel: AppModel     var body: some View {         NavigationSplitView {             SidebarView()         } detail: {             PersonView(person: appModel.selectedPerson)         }     } } struct SidebarView: View {     @EnvironmentObject private var appModel: AppModel     private let persons = [Person(firstname: "Joe"), Person(firstname: "Jane")]     var body: some View {         List(persons, id:\.self, selection: $appModel.selectedPerson) { person in             Text(person.firstname).tag(person)         }         .listStyle(.sidebar)     } } struct PersonView: View {     let person: Person?     var body: some View {         if let person {             Text(person.firstname)         }         else {             Text("No Selection")         }     } }
10
2
2.8k
Jul ’23
SwiftUI toolbar item keyboard shortcut not showing when holding command key on iPad?
How come keyboard shortcuts associated to toolbar items do not show up when you hold down the command key? import SwiftUI struct ContentView: View { var body: some View { NavigationStack { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") Button { } label: { Text("Tap Me!") } .keyboardShortcut("o", modifiers: .command) } .padding() .toolbar { ToolbarItem { Button { } label: { Image(systemName: "bolt") } .keyboardShortcut("k", modifiers: .command) } } } } } See screenshot. The shortcut for "Tap Me!" is being shown but not the one for the toolbar item. Am I doing something wrong or this is just not supported yet in SwiftUI? If that's the case, that seems to be a significant omission.
2
0
1.4k
Jul ’23
Symbol not found when running iPad app in Vision Pro Simulator
When I try to run my iPad app in the simulator, I get this error: dyld[25133]: Symbol not found: _$s21DeveloperToolsSupport15PreviewRegistryPAAE04makeD0AA0D0VyKFZ Referenced from: <40E6A0C8-6B05-3C87-8F68-D333EF2586EA> /Users/me/Library/Developer/CoreSimulator/Devices/DAE1996F-4A59-49CA-B55D-FE2AF0E6461F/data/Containers/Bundle/Application/B49E91FE-89D5-4496-831D-31B87A6F1170/My.app/My Expected in: <31FB64EE-D651-3287-9607-1ED38855E80F> /Library/Developer/CoreSimulator/Volumes/xrOS_21N5165g/Library/Developer/CoreSimulator/Profiles/Runtimes/xrOS 1.0.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/DeveloperToolsSupport.framework/DeveloperToolsSupport Message from debugger: killed Is this a bug, my iPad app is not compatible or there's something I need to do?
4
1
1.2k
Jul ’23
Swift compiler crash in Xcode 15 beta 3 (15A5195k)
Somehow the Swift compiler is unable to this code: @Observable class ServiceBrowser: NSObject { typealias ResolveServiceCompletionBlock = (Bool, Error?) -> Void fileprivate var resolveServiceCompletionHandler: ResolveServiceCompletionBlock? = nil } Here's the crash: 4 . While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/luc/Work/Repositories/app-shared/App/Shared/Connectivity/ServiceBrowser.swift") 5 . While silgen init accessor SIL function "@$s7App14ServiceBrowserC07resolveB17CompletionHandler33_5B15C352D9CC926D1F8A0ECAC5970199LLySb_s5Error_pSgtcSgvi". for init for resolveServiceCompletionHandler (at /Users/luc/Work/Repositories/ap-shared/App/Shared/Connectivity/ServiceBrowser.swift:86:21) 6 . While emitting reabstraction thunk in SIL function "@$sSbs5Error_pSgIegyg_ytIegd_TR".
2
0
1.3k
Jul ’23
SwiftUI window aspect ratio
Is there a way to force a window to resize according to an aspect ratio? This is possible in AppKit via the NSWindow.contentAspectRatio property but I cannot find something similar for SwiftUI. Basically, I want the window to maintain the aspect ratio of its contents.
4
2
2.5k
Jun ’23
ShareLink and DataRepresentation
In my app, I'd like to be able to share a .csv file via ShareLink and Transferable. I watched the "Meet Transferable" WWDC22 video and it should be possible as the presenter demonstrated that use case. However, when I try this on my end, I am able to share the content but somehow it is treated by iOS as plaintext and when sharing by email or messages, it will just add the text content to the body. If I try to share via AirDrop, it creates a random filename with the .txt extension even though I specify .commaSeparatedText. The only way this somewhat works is when saving to files. It will save as a .csv file but the filename is set to "comma-separated values". Here's some code: struct MyArchive {     enum ValidationError: Error {         case invalid     }     var filename: String { return "myarchive.csv"     }          init(csvData: Data) throws {         //...     }     func convertToCSV() throws -> Data {         let test = "Name,Email\nPete,pete@example.com"         if let data = test.data(using: .utf8) {             return data         }         else {             throw ValidationError.invalid         }     }  } extension MyArchive: Transferable {     static var transferRepresentation: some TransferRepresentation {         DataRepresentation(contentType: .commaSeparatedText) { archive in             try archive.convertToCSV()         } importing: { data in             try MyArchive(csvData: data)         }     } } And in my View: struct View: View { var body: some View { //... let csv = MyArchive() ShareLink( item: csv, preview: SharePreview(                         csv.filename,                         image: Image(systemName: "doc.plaintext")                     ) ) } } I'm at the point that I wonder if I'm doing something wrong or this just doesn't work in iOS 16 beta 1. Any hints? Thanks!
5
0
4.2k
May ’23
Can't set focus with tab key when text fields are in a form (iPadOS)
I don't know if this is a bug but when you put text fiels in a form, pressing the Tab key on a physical keyboard will just jump to the first field and stay there: import SwiftUI struct ContentView: View { enum FocusedField { case firstName, lastName } @State var name = "" @State var lastName = "" @FocusState private var focusedField: FocusedField? var body: some View { Form { TextField("Name", text: $name) .focused($focusedField, equals: .firstName) TextField("Last", text: $lastName) .focused($focusedField, equals: .lastName) } .padding() .onAppear { focusedField = .firstName } } } This is the result: Note that this works fine on macOS or when using the Tab key on the virtual keyboard. If you put the fields into a VStack instead, you'll be able to move through the fields via the Tab key.
2
1
1.7k
Mar ’23
TableColumn with text and image
I'm trying to display a Label in a TableColumn but the header is not rendered properly: Here's some code: struct Computer: Identifiable { let id: UUID let name: String init(_ name: String) { id = UUID() self.name = name } } struct ContentView: View { private var computers = [Computer("iMac"), Computer("MacBook"), Computer("Mac mini")] @State private var selectedComputers = Set<Computer.ID>() @State private var sortOrder = [KeyPathComparator(\Computer.name)] var body: some View { Table(computers, selection: $selectedComputers, sortOrder: $sortOrder) { // Header rendered incorrectly TableColumn("Name", value: \.name) { computer in Label(computer.name, systemImage: "desktopcomputer") } // This works: // TableColumn("Name", value: \.name) } } } If I use a Text element instead (or not define any custom view for the TableColumn), the header is rendered properly: Am I doing it wrong or this is a bug?
1
0
1.2k
Feb ’23
Placeholders for text fields embedded in forms (macOS)
It seems like there's no way to set a placeholder in a text field when it is in a form on macOS. This is not an issue on iOS. import SwiftUI struct ContentView: View { @State private var textUp = "" @State private var textDown = "" var body: some View { VStack { Form { Text("In a form:") .fontWeight(.bold) TextField("placeholder", text: $textUp) } Text("Not in a form:") .fontWeight(.bold) TextField("placeholder", text: $textDown) } .padding() } } Am I missing something or is this just not supported?
1
0
1.2k
Feb ’23
Buttons with label won't show in keyboard shortcuts list on iPad
Why won't a button using a Label show in the keyboard shortcuts list (holding ⌘ key)? struct ContentView: View { var body: some View { NavigationSplitView { SidebarView() } detail: { Text("Hello, world") } } } struct SidebarView: View { let items = [1, 2, 3, 4, 5, 6] var body: some View { List(items, id:\.self) { item in Text("\(item)") } .toolbar { Button { // Do something } label: { Label("New", systemImage: "plus") } .keyboardShortcut("n") // Will NOT show in shortcuts list Button("Play") { // Do something } .keyboardShortcut("p") // Will show in shortcuts list } } } Am I doing something wrong or is this just yet another SwiftUI shortcoming?
2
0
657
Dec ’22
macOS: Impossible to set focus on a textfield embedded in an alert
As of now, it seems impossible to set focus on a text field that is embedded in an alert on macOS. struct ContentView: View { @FocusState private var focusedField: FocusField? @State private var showingAlert = false @State private var name = "" enum FocusField { case folderName } var body: some View { VStack { Button { // focusedField = .folderName // Not working either showingAlert.toggle() } label: { Text("Show alert") } .alert("Alert", isPresented: $showingAlert, actions: { TextField("Name", text: $name) .font(.body) .autocorrectionDisabled() .focused($focusedField, equals: .folderName) Button("Cancel", role: .cancel, action: {}) }) #if os(macOS) .defaultFocus($focusedField, .folderName) #endif } .padding() } } When running this code on iOS, the text field does get focus automatically. Is it me that is doing something wrong or it's just a SwiftUI shortcoming on macOS?
1
0
653
Dec ’22
Generic parameter 'SelectionValue' could not be inferred
Hi! What is wrong with this code and why does this work for a Picker but not a List? struct ContentView: View {     enum FooBar: CaseIterable, Identifiable {         public var id : String { UUID().uuidString }                 case foo         case bar         case buzz         case bizz     }     @State var selectedFooBar: FooBar = .bar     var body: some View {         VStack {             Picker("Select", selection: $selectedFooBar) {                 ForEach(FooBar.allCases) { item in                     Text(self.string(from: item)).tag(item)                 }             }                         List(FooBar.allCases, selection: $selectedFooBar) { item in                 Text(self.string(from: item)).tag(item)             }                         Text("You selected: \(self.string(from: selectedFooBar))")         }     }     private func string(from item: FooBar) -> String {         var str = ""                  switch item {         case .foo:             str = "Foo"                      case .bar:             str = "Bar"                      case .buzz:             str = "Buzz"                      case .bizz:             str = "Bizz"         }         return str     } }
3
0
3.6k
Sep ’22