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?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Created
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.
I'm trying to create a List that allows multiple selection. Each row can be edited but the issue is that since there's a tap gesture on the Text element, the list is unable to select the item.
Here's some code:
import SwiftUI
struct Person: Identifiable {
let id: UUID
let name: String
init(_ name: String) {
self.id = UUID()
self.name = name
}
}
struct ContentView: View {
@State private var persons = [Person("Peter"), Person("Jack"), Person("Sophia"), Person("Helen")]
@State private var selectedPersons = Set<Person.ID>()
var body: some View {
VStack {
List(selection: $selectedPersons) {
ForEach(persons) { person in
PersonView(person: person, selection: $selectedPersons) { newValue in
// ...
}
}
}
}
.padding()
}
}
struct PersonView: View {
var person: Person
@Binding var selection: Set<Person.ID>
var onCommit: (String) -> Void = { newValue in }
@State private var isEditing = false
@State private var newValue = ""
@FocusState private var isInputActive: Bool
var body: some View {
if isEditing {
TextField("", text: $newValue, onCommit: {
onCommit(newValue)
isEditing = false
})
.focused($isInputActive)
.labelsHidden()
}
else {
Text(person.name)
.onTapGesture {
if selection.contains(person.id), selection.count == 1 {
newValue = person.name
isEditing = true
isInputActive = true
}
}
}
}
}
Right now, you need to tap on the row anywhere but on the text to select it. Then, if you tap on the text it'll go in edit mode.
Is there a way to let the list do its selection? I tried wrapping the tap gesture in simultaneousGesture but that didn't work.
Thanks!
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?
I'm in the process of migrating to the Observation framework but it seems like it is not compatible with didSet. I cannot find information about if this is just not supported or a new approach needs to be implemented?
import Observation
@Observable class MySettings {
var windowSize: CGSize = .zero
var isInFullscreen = false
var scalingMode: ScalingMode = .scaled {
didSet {
...
}
}
...
}
This code triggers this error:
Instance member 'scalingMode' cannot be used on type 'MySettings'; did you mean to use a value of this type instead?
Anyone knows what needs to be done? Thanks!
Is .refreshable supposed to do anything on macOS? Works fine on iOS and iPadOS but it's not triggered on macOS. It's available since macOS 12 but the documentation doesn't mention anything about that.
https://developer.apple.com/documentation/swiftui/view/refreshable(action:)
Is there no way to hide the pointer with SwiftUI? If so, how?
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?
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".
DNSServiceNATPortMappingCreate returns external port 0 since macOS 13 b3 build 23A5286i.
This was the case in macOS 12 as well (see FB9137313 and FB9139688).
Hopefully this won't take months after macOS is released before it is fixed like last time.
Latest radar: FB12579235
Also, see https://developer.apple.com/forums/thread/681836
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.
Since iOS/iPadOS beta 6, I get a crash just by simply selecting an item in the sidebar of a navigation view. The selected item is part of an app mode, with conforms to the Observation protocol:
import SwiftUI
import Observation
@main
struct MREAApp: App {
@State private var appModel = AppModel.shared
var body: some Scene {
WindowGroup {
ContentView()
.environment(appModel)
}
}
}
@Observable class AppModel {
static var shared = AppModel()
var selectedFolder: Folder? = nil
}
struct Folder: Hashable, Identifiable {
let id = UUID()
let name: String
}
struct ContentView: View {
@Environment(AppModel.self) private var appModel
var folders = [Folder(name: "Recents"), Folder(name: "Deleted"), Folder(name: "Custom")]
var body: some View {
NavigationSplitView {
SidebarView(appModel: appModel, folders: folders)
} detail: {
if let folder = appModel.selectedFolder {
Text(folder.name)
}
else {
Text("No selection")
}
}
}
}
struct SidebarView: View {
@Bindable var appModel: AppModel
var folders: [Folder]
var body: some View {
List(selection: $appModel.selectedFolder) {
ForEach(folders, id: \.self) { folder in
NavigationLink(value: folder) {
Text(folder.name)
}
}
}
}
}
To reproduce the bug, just tap on one of the item.
Oddly enough, this works fine in the Simulator.
macOS 14 beta 5 is not affected either.
Apple folks: FB12981860
Like title says. Why make users jump through hoops on macOS to redeem a code while it's trivial on iOS?
At least make the button open the Mac App Store app to the redeem sheet!
FB13202274
Is it possible to have sections in a SwiftUI table? I cannot find any documentation or examples that show that it is.
I've spent 3 days moving localization from my older project to my new project and converted to Strings Catalog.
When I tried to export, I got the Unable to build project for localization string extraction error.
Read about that and the workaround seems to be to set Localisation Export Supported to NO, which I did.
I worked once and now it refuses to export and complains about missing targets, which I had to remove from the export process as recommended. WTH?
Or, at some point it complained about a missing Swift Package.
wwdc2023-10155