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

SwiftUI Documentation

Post

Replies

Boosts

Views

Activity

SwiftUI - Horizontal Scroll not Working with NSTextView in NSViewRepresentable
Hello, I have an NSViewRepresentable displaying an NSTextView in SwiftUI. GeometryReader() { geometry in ScrollView([.vertical, .horizontal]) { Representable() // -18 for vertical scroller .frame(minWidth: geometry.size.width - 18) } } When I use the vertical scrollbar the mouse click is passed to the Representable and is not registered by the Scroll bar. In the image you can see I tried to use the scroll bar and it highlighted some text instead. There is a suggestion on stackoverflow to use an overlay. This fixes the scrolling issue, but will then make the Representable uninteractable. How can I use the horizontal scroll bar here as expected?
1
0
193
4w
Problem with ScrollView
Hi guys, I have a really odd problem. Whenever I try to add a vertical ScrollView, VStack or a LazyVStack to this code everything disappears. Anyone have an idea why this is happening and how to fix it? No errors show up in my code. GeometryReader{ geometry in VStack(alignment: .center, spacing: 100){ ScrollView(.horizontal, showsIndicators: false){ HStack(spacing: 250){ CardView1( showOnlyImage: false, imagename1: "TheMiceGalaxiesHubble", heading1: "The Mice Galaxies", description1: "Located in the constellation Coma Berenices." ) .scaleEffect(getScale(proxy: geometry)) .animation(.easeOut(duration: 0.3), value: getScale(proxy: geometry)) CardView1( showOnlyImage: false, imagename1: "TheMiceGalaxiesHubble", heading1: "The Mice Galaxies", description1: "Located in the constellation Coma Berenices." ) .scaleEffect(getScale(proxy: geometry)) .animation(.easeOut(duration: 0.3), value: getScale(proxy: geometry)) } } .frame(height: -200) ScrollView(.horizontal, showsIndicators: false){ HStack(spacing: 250){ CardView1( showOnlyImage: false, imagename1: "TheMiceGalaxiesHubble", heading1: "The Mice Galaxies", description1: "Located in the constellation Coma Berenices." ) .scaleEffect(getScale(proxy: geometry)) .animation(.easeOut(duration: 0.3), value: getScale(proxy: geometry)) } } } } } .contentMargins(50, for: .scrollContent) .scrollTargetBehavior(.viewAligned) .scrollTargetLayout() .padding(.horizontal, 10) .padding(.top, -35) .padding(.bottom, 50) } } } } }
0
0
127
4w
PersonNameComponents TextField Not Responding As Expected on macOS
Using this adapted code snippet from the Apple Developer Documentation, struct ContentView: View { @State var nameComponents = PersonNameComponents() var body: some View { Form { TextField( "Proper name", value: $nameComponents, format: .name(style: .medium) ) .onSubmit { } .disableAutocorrection(true) .border(.secondary) Text(nameComponents.debugDescription) } } } It runs and works as expected on my iOS device. When I use this same code in my macOS app, it has unexpected results. The validation mechanism (likely caused by format: .name(style: .medium) in order to bind the TextField to a PersonNameComponents instance, will not let me add spaces or delete all the text in the TextField. I have to write the first name in this format: "FirstLast" and then add a space in between the t and L. Are there any ways I can fix this on macOS while still binding my TextField to a PersonNameComponents instance?
0
0
150
4w
I can’t understand this SwiftUI compile error.
Text("Sample Text with wrong font type") .font(.bold) I know this code is incorrect, and it returns an error stating that .font doesn’t have a .bold type. However, when I place this incorrect code within a stack… …no error message appears. Instead, I get: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions when trying to run the code. It’s unclear why the compiler doesn’t specify that the issue originates from .font(.bold). This was confusing, and I spent 20 minutes figuring out it was a typo.
3
0
193
4w
SwiftUI Previews creating views out of thin air?
My iOS (iPhone/iPad/Mac Catalyst) app has a relatively complex authentication flow, primarily required due to it being an API client. Several of my views have .task modifiers that begin executing code when the views are created. After moving to iOS 18 and Xcode 16, when running in the Simulator, I began noticing my workflow was breaking as views were being created out of sequence... but not by me 🤯 Here is an example stack when I place a breakpoint inside of this view's .task. From my logging (and this stack) I can verify I am not creating this view. This view has no preview associated with it, and there are no previews up the view hierarchy from it either. This happens in the Simulator only The only clue I have to go on is this line of text which is present at the top of what looks like ASM when I click on the (5) line in the stack above. SwiftUI`(1) await resume partial function for dispatch thunk of static SwiftUI.PreviewModifier.makeSharedContext() async throws -> τ_0_0.Context: If this (or any hierarchical view) used Previews, I'd remove them to see if it had an effect, but they don't! I'm at a loss on what to do here...
8
0
240
4w
CPU use increases over time with simple animation
I created a simple animation of a circle that changes sizes. The circle pulses like a heartbeat in the center of the screen. My expectation was for the CPU use to be very low, but that is not the case. In addition, even if the CPU use isn't as low as I would expect, I did not expect the CPU use to increase over time because nothing else is happening in the app. Here is the code: import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { SplashScreenView() } } } import SwiftUI struct SplashScreenView: View { var body: some View { ZStack { SplashNucleusView(minSize: 50, maxSize: 100) } } } import SwiftUI struct SplashNucleusView: View { let minSize: Double let maxSize: Double @State private var nucleusColor: Color = .primary @State private var nucleusRadius: Double = 10 @State private var nucleusOpacity: Double = 1.0 private var nucleusAnimation: Animation { .easeInOut(duration: 0.25) .repeatForever(autoreverses: true) } let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect() var body: some View { Circle() .fill(nucleusColor) .frame(width: nucleusRadius) .opacity(nucleusOpacity) .onReceive(timer) { _ in withAnimation(nucleusAnimation) { nucleusRadius = Double.random(in: minSize...maxSize) } } } } This is how the animation looks: The animation is snappy until the CPU use reaches 95%, at which point there is visible stuttering. Here is how the CPU looks when the animation duration value is 0.5 seconds and the timer publishing interval is 3 seconds: Changing the animation duration value to 0.25 seconds and the timer publishing interval to 0.5 seconds changes the CPU use as follows: The complete view has many other moving parts, which make the issue much worse. The issue is evident with only the circle view. I spent hours working with the debugger, reading about animations, and testing new approaches, but the result is always the same. Why is this happening?
2
0
209
4w
Loading object on NSItemProvider is delayed until drop exit
I have drag-and-drop functionality in the macOS app built with SwiftUI. Since macOS 15 there is an issue with it, because as I found out, the completion block of loadObject method on NSItemProvider is not called until dropExited delegate method is called (see simplified code example below). It causes very strange behavior in my app, for one of the most important features, and I am looking for a way to fix it as soon as possible. Is anyone seeing the same issue? I saw there was a bug with drag and drop on iOS 18, which seems to be fixed in 18.1. Anyone from Apple can say anything about this change in behaviour? @Observable // Because it is injected via environment. final class DragAndDropDelegate<T: Codable>: DropDelegate { func dropEntered(info: DropInfo) { // Is called as expected. guard let itemProvider = info.itemProviders(for: [UTType.data]).first else { return } itemProvider.loadObject(ofClass: DraggableObject<T>.self) { [weak self] (object, error) in // This is only called after dropExited delegate method is called !!! // Before macOS 15.0 it is called quickly after the loadObject method invokation. asyncOnMainThread { guard let self, let draggableObject = object as? DraggableObject<T> else { return } self.onEnter?(draggableObject.object, info.location) } } } func dropExited(info: DropInfo) { // Is called as expected. } }
4
0
165
4w
How would you make a View that magnifies the View(s) beneath it?
I need a magnifying glass function for one of my SwiftUI Views, but can't find a way to implement it as needed. I found a Youtube video where the author renders the view twice, overlaying the second over the first, then scaling and masking it to create the illusion of magnification, but this is expensive and doesn't work in many cases where more complex views are presented (e.g. a LazyVGrid). I've also explored continually capturing partial screenshots and scaling them up to create the illusion of magnification, but there's no straightforward way to achieve this with SwiftUI without getting into the messiness of UIViewRepresentables. Any help would be greatly appreciated
2
0
153
4w
Text right-to-left direction with mathematical text
Hi, I am writing a calculator app in Swift 5 and decided to support eastern asian numerals to make it a bit interesting. The numeric display itself works well in Arabic langauge setting, but I have a protocol view that reports the entered data and results. I do it in a Grid with two Text items, one for the input and one for the result. The result contains a numeric string in U0660... arabic numerals, and that looks fine. The input is a construct looking like Text(number1) + Text(" ") + Text(operation), so it goes into a single Grid cell. The standard version looks like this: but the arabic version shows the numbers and operation symbols in the wrong sequence: I guess that has something to do with the mathematical symbols such as + and = simply register as ltr Text and confuse the text layout. If I localize π, the result looks different: So my question would be: How do I force a fragment such as Text(" +") to go in rtl direction? Is this a SwiftUI topic or doesit go deeper? I am looking at the Text element now, but may be that is the wrong approach.
6
0
172
4w
AppIntentTimelineProvider "func timeline(for" is called twice after a widget button triggers an AppIntent Perform
I'm adding widget interactivity to my home screen widgets via buttons and AppIntents, but running into some interesting behavior the way the timeline is reloaded after. I'm following this guide from Apple https://developer.apple.com/documentation/widgetkit/adding-interactivity-to-widgets-and-live-activities And the widget is guaranteed to be reloaded when a button pressed with an intent, But whenever the AppIntent is done with the perform action, the widget timeline is always reloaded twice. It's also interesting to note that both reloads happen after the perform method. If you add a 10 second sleep in the perform, nothing happens for 10 seconds, then both reloads happen. This issue with this is 2-fold. calculating and rendering the entire widget timeline can be Networking and DB intensive operations, so I would ideally like to avoid doing all the work twice and save the users battery and processing. The even worse issue, sometimes data on the server changes in between the split second duplicate widget timeline reloads, causing the widget to flash one state, then update to another a second later which is not a good user experience. I have a sample project which shows the issue and is very easy to reproduce. The widget simply keeps track of the number of reloads. To reproduce: Add the widget to the homescreen Press the refresh button, and observe the timeline refresh count always goes up by 2. I've filed a Feedback and attached the sample project and screen recording for anyone to reproduce. FB15595835
1
0
329
Oct ’24
Is it intended for onchange to work with let properties?
Hi all, by chance, we introduced code similar to the following in the body of one of our views: struct MyView: View { let myInt: Int var body: some View { EmptyView() // Empty body .onChange(of: myInt) { newValue in … } } } Notice that a simple let of type Int is used here, rather than State or something similar. While it works and onChange is actually triggered when views are rebuilt, I’d like to clarify a few things: Is this behavior actually expected and reliable, or is it a gray area and can be changed in the future? If it is expected, can this be made clearer in the documentation? If it is a gray area, can the documentation be improved to address it? Thanks!
1
1
153
Oct ’24
Re-orderable items in a ScrollView?
I would have thought this was trivial to do in SwiftUI, but apparently not. Imagine a list of Items, each with some text in, within a ScrollView or something which scrolls (VStack maybe). The list goes off the screen so the user can scroll to see the bottom. I want to be able to re-order that list as well, by dragging Items. Let's assume a one sec delay before it lets you 'pick up' the Item, and then drag to re-order. The delay is so as not to conflict with scrolling. And the dragged item should look exactly like the non-dragged item (except maybe a shadow or outline or something) Is this possible with SwiftUI?
1
0
151
Oct ’24
Question about .presentationDetents and interactiveDismissDisabled modifiers
I have an app that I'm working on where I'd like to use the .presentationDetents modifier and I need the view to not be dismissed by the user. I'm therefore using the .interactiveDismissDisabled modifier. Unfortunately, that means that the Map on the ContentView can no longer respond to touch gestures. How can I have a similar experience to the Maps app where the underlying Map (or other views) can be interacted with and the user can still be prevented from dismissing the view controlled by the .presentationDetents modifier? SomeView(searchResults: $searchResults, selectedItem: $selectedItem) .presentationDetents([.height(100), .medium]) .presentationDragIndicator(.visible) .interactiveDismissDisabled(true)
2
0
212
Oct ’24
Failed to preview SwiftUI app for macOS in Xcode 12.(0, 1, 3, 4).
Hello! SwiftUI preview does not work. I tried different versions of Xcode, deleting the "Developer" folders, clearing the "Build" folder from the project in Xcode, reinstalling Command Line Tools and Developer Tools. I think that the error appeared after I cleaned Xcode with CleanMyMacX, before that the preview worked. -MacBook Pro (15-inch, Mid 2012) -macOS Catalina 10.15.7 -Xcode 12.3 Thanks for the feedback!
1
0
199
Oct ’24
How to make a FocusState on WatchOS
Hey guys, I'm creating a fitness app on WatchOS and I need to realize a focus on a variable but it doesn't work. I will show you my code to understand the problem : @State var WeightChoose: Double = 0.0 @FocusState private var isWeightActive: Bool var body: some View { HStack { Text("Poids:") Spacer() Text("\(String(format: "%.0f", WeightChoose)) KG") .focused($isWeightActive) .foregroundColor(isWeightActive ? .green : .white) .digitalCrownRotation($WeightChoose, from: 0.0, through: 300.0, by: 1.0, sensitivity: .medium) .animation(.easeIn(duration: 1), value: WeightChoose) .padding() Here my code don't show me any error but the focus doesn't work at all. Before, I used the old way to focus with @State private var isWeightActive = false instead of @FocusState and .focusable(true) { focused in isWeightActive = focused } Instead of .focused() But now the .focusable() method is depreciated since watchOS 8 and we have to go on FocusState and .focused but it doesn't work for my code. If you could help me with that it would be awesome. Thank you Cyrille
1
0
213
Oct ’24
不清楚iOS照片的某些权限是否有被开放
我想开发的是一款照片相关软件~ 主要想知道 1.可以从我的 APP 跳转到 iOS 的照片并且定位到选择的照片/视频吗? 2. PHPickerViewController 没有全选的功能,这里面的精选集也无法全选添加到我开发的 APP,这里面有各种 iOS 照片已经整理归类好的内容~如:人物。我是可以通过代码自建picker 来调用到这些合集里的内容来全选添加吗~好像并没有看到相关的 API~ 求求哪个大神指教下
1
0
113
Oct ’24
VoiceOver issues with LazyVStack in ScrollView
I have a ScrollView which has inside a LazyVStack and multiple rows. On the appearing of the ScrollView, I scroll to row 250. When having the VoiceOver running, after opening the app, the focus is on the navigation title as it should be, than, after swiping right to enter the ScrollView, one would expect the focus to be placed on the first visible row, but this is not the case, it scrolls back to some row and focus on that, than reads it. Here is the code to reproduce it, and if you ask me, this is pretty standard stuff I don't do anything special. import SwiftUI struct ContentView: View { var body: some View { NavigationView { ScrollViewReader { scrollProxy in ScrollView { LazyVStack { ForEach(1...500, id: \.self) { index in NavigationLink(destination: DetailView(row: index)) { Text("Row \(index)") .padding() .frame(maxWidth: .infinity) .background(Color.gray.opacity(0.2)) .cornerRadius(8) .padding(.horizontal) } } } } .onAppear { // Scroll to row 250 when the view appears withAnimation { scrollProxy.scrollTo(250, anchor: .center) } } } .navigationTitle("Rows") } } } struct DetailView: View { let row: Int var body: some View { VStack { Text("Detail for Row \(row)") .font(.largeTitle) .padding() Spacer() } .navigationTitle("Row \(row) Details") } } @main struct ScrollViewExampleApp: App { var body: some Scene { WindowGroup { ContentView() } } }
0
0
144
Oct ’24