SwiftUWhy
Welcome to my new series on things I don’t understand about Apple’s premier user interface framework.
[…]
To be clear, these are things I don’t understand, not necessarily things that are “wrong.” They sure look wrong (or at least “suboptimal”) to me! But maybe there are good reasons, and I just don’t know them yet. SwiftUI experts and historians, please tell me!
Previously:
Update (2025-01-10): John Siracusa (GitHub):
Why is there (apparently?) no
Pickerstyle on macOS that can be configured with a fixed width, likeNSPopUpButtonin AppKit?For example, consider a text field for numeric input followed by a picker for units, both aligned to the right side of a System Settings-style Form. Each time you pick a different unit, the
Pickerchanges width and moves the text field.
Q: Why is Xcode no longer able to render a preview of my SwiftUI view?
A: Sometimes, the answer is that you’ve got a call to a log method on a
Loggerobject in a code path that’s executed when rendering a preview but rarely or not at all otherwise, and this call passes a string without any values interpolated into it (e.g.,logger.log("Hello")). This causes an error, and this error is only visible in the big spew of output available through the stethoscope icon in the preview pane.
ScrollViewon macOS includes default support for swipe gestures and mouse scroll wheels, but not page up/down, home/end, or arrow keys?
See also: the Mastodon tag.
Update (2025-01-17): John Siracusa:
Why can’t I find a simple way to do this?
.textSelection(allowSelection ? .enabled : .disabled)
Update (2025-02-11): John Siracusa:
Xcode should track the lifetime number of hours spent trying to find the error in your SwiftUI view code that is causing the “compiler is unable to type-check this expression in reasonable time” message to appear after the obligatory a 60-second wait.
To be clear, this message is almost never (directly?) related to type checking when it happens in a SwiftUI View. Instead, it’s caused by things like a typo in the parameter name of a method call.
“A typo in a parameter name? Why, that should be pinpointed instantly by the complier!” Sure, in normal code. But in a SwiftUI view, you get to try to find it yourself, with no help! Each try costs you 60 seconds.
Update (2025-06-20): John Siracusa:
This project demonstrates a SwiftUI layout bug. The code includes a
TabViewcontaining aFormthat has.fixedSize(horizontal: false, vertical: true)set on it. This should cause the containingTab,TabView, and (ultimately) window to allocate enough vertical space so that there is no scrolling in this view.This works…until a trivial
NSViewRepresentableview (InnocentNSView) is added to theZStackthat contains theTabView. The mere presence of this entirely emptyNSViewRepresentableview causes the height calculation to be off by a pixel or two, which causes theFormto have a tiny bit of scrolling.