Explore the various UI frameworks available for building app interfaces. Discuss the use cases for different frameworks, share best practices, and get help with specific framework-related questions.

All subtopics

Post

Replies

Boosts

Views

Activity

SwiftUI Map Annotation Coordinate Animation
Hi, I have a SwiftUI Map with a set of three annotations. These annotations move around, and I would like to animate their movement from one coordinate to another, but I'm not finding a way to do that. I've tried using withAnimation { } when setting my array of Identifiable models that back the Annotations, and I've tried adding the .animation(.default, annotationModels) modifier to my Map object (where annotationModels is the array that backs my Annotations). The animation modifier doesn't work on Annotation structs, and it doesn't work if I add the animation modifier within the Annotation's view either. Does anyone have any suggestions on how I might be able to animate the coordinates of annotations using a SwiftUI Map? Does the problem have to do with the fact that I have an array of these annotations?
0
0
45
7h
Infinite loop using NavigationStack
Hello. I've seen some other posts about NavigationStack, but my variation seems a little different. FYI, I have not migrated from ObservableObject to Observable yet. However, having a path in either seems to be a factor in this issue. My code has no issues when built with Xcode 15. When built with Xcode 16 I keep hitting scenarios where the .onAppear for my first tab gets called over and over again endlessly. If I go to my second tab, which uses a NavStack with a path and then navigate anywhere my .onAppear for my FIRST tab gets call endlessly. I’ll sometimes see a “double push” to the stack. (Someone posted a video of this happening on Mastodon, which apparently I’m not allowed to link to here.) The second tab is accessing the path property via an @EnvironmentObject. I can stop this endless loop by removing @Published from the property in my ObservableObject that holds my path. But then if I go to my third tab, which does NOT use a path, the .onAppear for my FIRST tab again gets called endlessly. So far on Mastodon I’ve seen three people encountering problems possibly related to storing a path in something being observed. Feedback requires a sample project, which I am having trouble creating to show the problem.
1
0
59
11h
Swift Chart Zoom/Magnification/Pinch Gesture
Is there any approach or sample code available to use these APIs: .chartScrollableAxes(.horizontal) .chartScrollPosition(x: ...) .chartScrollPosition(initialX: ...) .chartScrollTargetBehavior(...) .chartXVisibleDomain(length: ...) Plus a gesture recognised or Pinch or Magnification to create a Swift Chart with an X axis that can be zoomed in or out with a pinch gesture? And when zoomed in at any level above 0, the chart can then be scrolled left to right along the X axis. I've had success using .chartScrollableAxes with .chartXSelection in parallel, so would also like to keep the ability to select X values too.
0
0
72
12h
UITableViewDiffableDataSource apply snapshot crashes on iOS 17
Hello all, We got app crashes on dataSource.apply(snapshot, animatingDifferences: false) after refactoring our tableView code to UITableViewDiffableDataSource. This doesn't happen every time. DispatchQueue.main.async { [weak self] in guard let self = self else { return } var snapshot = getCurrentSnapshot() snapshot.deleteItems([...]) snapshot.appendItems([...], toSection: section) dataSource.apply(snapshot, animatingDifferences: false) } And the controller has deallocated before it. Crash log: controller (0x110a64800): deallocated *** Assertion failure in NSInteger _UITableViewRowDataNumberOfRowsInSection(UITableViewRowData *__unsafe_unretained, NSInteger)(), UITableViewRowData.m:1761 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requested the number of rows for section (4) which is out of bounds.' *** First throw call stack: ( 0 CoreFoundation 0x0000000180491128 __exceptionPreprocess + 172 1 libobjc.A.dylib 0x000000018008412c objc_exception_throw + 56 2 Foundation 0x0000000180d11770 -[NSMutableDictionary(NSMutableDictionary) classForCoder] + 0 3 UIKitCore 0x0000000185538ad0 -[UITableViewRowData numberOfRowsInSection:] + 260 4 UIKitCore 0x00000001855052f0 -[UITableView _shouldDrawSeparatorAtBottomOfSectionForCellAtIndexPath:] + 152 5 UIKitCore 0x0000000185504dbc -[UITableView _updateSeparatorStateForCell:atIndexPath:] + 100 6 UIKitCore 0x0000000185504cf0 -[UITableView _updateSeparatorStateForVisibleCells] + 188 7 UIKitCore 0x00000001854f4bd0 -[UITableView _updateAnimationDidStopWithOldVisibleViews:finished:context:] + 1108 8 UIKitCore 0x00000001854f2630 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke_4 + 36 9 UIKitCore 0x00000001857fcb1c __UIVIEW_IS_EXECUTING_ANIMATION_COMPLETION_BLOCK__ + 28 10 UIKitCore 0x00000001857fce24 -[UIViewAnimationBlockDelegate _sendDeferredCompletion:] + 100 11 libdispatch.dylib 0x000000010e5100f0 _dispatch_call_block_and_release + 24 12 libdispatch.dylib 0x000000010e51193c _dispatch_client_callout + 16 13 libdispatch.dylib 0x000000010e5215e4 _dispatch_main_queue_drain + 1228 14 libdispatch.dylib 0x000000010e521108 _dispatch_main_queue_callback_4CF + 40 15 CoreFoundation 0x00000001803f1a30 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 16 CoreFoundation 0x00000001803ec148 __CFRunLoopRun + 1936 17 CoreFoundation 0x00000001803eb5a4 CFRunLoopRunSpecific + 572 18 GraphicsServices 0x000000018e9fbae4 GSEventRunModal + 160 19 UIKitCore 0x00000001852f02e4 -[UIApplication _run] + 868 20 UIKitCore 0x00000001852f3f5c UIApplicationMain + 124 21 appName 0x0000000103e8b0dc $s22appNameySpySpys4Int8VGGXEfU_ + 192 22 appName 0x0000000103e8b480 _swift_se0333_UnsafeMutablePointer_withMemoryRebound + 900 23 appName 0x0000000103e8af30 main + 1320 24 dyld 0x000000010e409544 start_sim + 20 25 ??? 0x000000010e61a0e0 0x0 + 4536246496 26 ??? 0xca54000000000000 0x0 + 14579277893705138176 ) libc++abi: terminating due to uncaught exception of type NSException
2
0
75
17h
[Live Activity / Dynamic Island] Countdown Timer issues (ending activity, styling)
Hello! I'm building a Countdown Timer for the Dynamic Island using a Live Activity. I have two issues for which I can't find any solution: We want to display the time in the "X minutes" format, like in this example. I went through the forum but all the answers were wrong, because they were using a Text(:format) which never updates in the live activity, or a Text(:timerInterval) which we can't format. I want the Live Activity to end once the timer gets to zero. I found this staleDate parameter that I thought would helped, but is actually only adding loaders on my design once the date is reached. I tried to implement a solution like the answer of this post, but the if (context.isStale) {...} part is never being rendered. It also looks like the stale sate gets activated only when the app is focus again. I tried several fixes, went through a lot of forum and posts, but I can't find any solution. Thanks!
0
0
46
17h
Modifying the view hierarchy of a UITableViewCell as part of reloadData
I am trying to debug a crash due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSFrozenDictionary layoutSublayers]: unrecognized selector sent to instance. I see 2 things that I find interesting about it. The fact that the instance is a __NSFrozenDictionary tells me that the reference to a CALayer that has since been evicted from memory and re-written. The call to layoutSublayers tells me that the CALayer was dealloc-ed at some point between the call to setNeedsLayout (or layoutIfNeeded) This seemingly occurs as part of a call to -[UITableView reloadData] Furthermore, each cell created by the UITableView has a UIStackView. As part of the call to cellForRowAtIndexPath the code adds an instance of "custom view" to the stack view's subviews. As part of the call to prepareForReuse the code removes the "custom view" from the stack view's subviews. Therefore as part of the prepareForReuse the "custom view" (and its layer) is evicted from memory. My theory is that the tableview does a layout pass on the visible cell which has since had its subview removed which causes the crash. My question is what are the constraints on when/where to call reloadData and/or when/where you should definitely avoid it as it relates to this context? This is code that modifies the view hierarchy of the cell as part of its lifecycle which AFAIK this is "not supported" since prepareForReuse is meant to be used for resetting view state and cellForRowAtIndexPath to reset content. In that sense, another question, are you not allowed to modify the cell view hierarchy period as part of the cycle to draw the visible cells or is it more of a case, do not call reloadData?
2
0
63
19h
.chartXScale not scaling domain of Chart as expected
Hi, I'm currently wrestling with the .chartXScale(domain:) modifier in order to get my Chart to display correctly. The basics of the Chart look like this. Chart(measurements, id: \.timestamp) { measurement in if let total = measurement.production?.total { BarMark( x: .value( "Timestamp", measurement.timestamp, unit: .weekOfYear, calendar: .current ), y: .value( "Solar production", total ) ) } } As anyone familiar with Charts can see, I sort data into columns based on what week of the year the measurements belong to. Some of them can be null, and when they are, I still want space in the Chart where a BarMark would've been to be taken up, like week number 4 in this example chart (in which I've defaulted all measurements that are null in week 4 to 0, for demonstration purposes): To achieve that, as I understand, I'm meant to use the .chartXScale(domain:) modifier, but when I apply the following modifier... .chartXScale(domain: firstDayOfMonth...firstDayOfNextMonth) ... (where the domain is from the first day of the month to the first day of the next month), to the code above, I end up with this weird half step when the last week of measurements are all null: For reference, here's how the domain dates are set in my minimum reproducible example: firstDayOfMonth = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: .now))! firstDayOfNextMonth = Calendar.current.date(byAdding: .month, value: 1, to: firstDayOfMonth)! Am I misusing this modifier somehow, or is this a bug? Would love some help figuring this out, thanks!
4
0
75
20h
[VisionOS] .onDelete gesture not triggered for mouse
I found a regression in Apple Vision OS 2.0 beta 3 (22N5277g). Previously, it was possible to trigger the .onDelete swipe gesture on a List/ForEach with a virtual mouse (i.e. remote desktop), however this is no longer possible (a finger pinch still works). Sample code to reproduce the issue: // // ContentView.swift // TextInputTest // import SwiftUI struct ListElement: Identifiable { let id = UUID() let content: String } struct ContentView: View { @State var list = [ ListElement(content: "a"), ListElement(content: "b"), ListElement(content: "c") ] var body: some View { List { ForEach(list) { element in Text(element.content) } .onDelete { _ in // No-op. } } } }
0
0
43
22h
UIRefresh not working for my project?
So for my project it's a SwiftUI Project however if I Add '@objc' to expose this instance method to Objective-C it doesn't want it for some reason and the other error code is Argument of '#selector' refers to instance method 'refreshWebView' that is not exposed to Objective-C mainly this is the error I need to fix thanks in advance this code is what's giving me the error refreshControl.addTarget(self, action: #selector(refreshWebView(_:)), for: UIControl.Event.valueChanged) this is for a pull to refresh the page
3
0
79
22h
[VisionOS] TextEditor does not respect .autocorrectionDisabled
Hello everyone, I wanted to double check with the UI Framework team to see if there's a mistake in my code or if there's some sort of regression. My goal is to have a user input text without any automatic modification of said text, namely remove auto-capitalization and auto-correct. // // ContentView.swift // TextInputTest // import SwiftUI struct ContentView: View { @State var text: String = "Enter some text here!" var body: some View { VStack { TextEditor(text: $text) .autocorrectionDisabled(true) .textInputAutocapitalization(.never) .font(Font.custom("SF Mono", size: 30)) .monospaced() .padding(20) } .padding() } } However, when typing the following: "This is a sentence" followed by a few spaces, a dot will automatically be inserted, making the string become: "This is a sentence.". Reproduced on: VisionOS 2.0 beta 3 (22N5277g)
1
0
51
22h
Can't figure out why Toolbar is not conforming to view
ToolbarItem in group is not conforming to view for some reason, here's my code: var body: some View { NavigationStack { Form { TextField("Company Name", text: $companyName) TextField("Role", text: $role) TextField("Location", text: $location) TextField("Yearly Salary", value: $yearlySalary, format: .currency(code: "USD")) .keyboardType(.decimalPad) } .navigationTitle("Add Application") .navigationBarTitleDisplayMode(.large) .toolbar { ToolbarItem(placement: .navigationBarLeading) { Button("back") { dismiss() } } ToolbarItem(placement: .navigationBarTrailing) { Button("Save") { let appdata = ApplicationData( companyName: companyName, role: role, location: location, yearlySalary: yearlySalary, dateApplied: dateApplied, notes: notes) // Save the application data dismiss() } } } } } The error reads: Static method 'buildExpression' requires that 'ToolbarItem<(), Button>' conform to 'View' I'm trying to change it up but I can't get past this error for some reason
2
0
93
1d
Live activities on watchOS not working on simulator
Is there a trick to getting the new Live Activities on Apple Watch working via the simulator? I have an iPhone paired to a watch simulator, but live activities created on the phone aren't appearing on the watch Smart Stack. It looks like maybe the Smart Stack isn't functional on the watch simulator, as there are a bunch of blank tiles where the Smart Stack should be.
0
0
56
1d
Section Fully Clickable in SwiftUI – NavigationLink Issue
Hi everyone, I’m having an issue with SwiftUI where my entire Section becomes clickable, instead of just the “Edit profile” button. Here’s my code: VStack(alignment: .center) { KFImage(authManager.user?.photoURL) .resizable() .scaledToFill() .frame(width: 80, height: 80) .clipShape(Circle()) Text(authManager.user?.displayName ?? "") .font(.system(.title, design: .rounded)) Text(authManager.user?.email ?? "") .font(.subheadline) .foregroundColor(.gray) NavigationLink { EditProfileView() } label: { Text("Edit profile") .frame(minWidth: 0, maxWidth: .infinity) .font(.system(size: 18)) .padding(10) .foregroundColor(.white) .background(Color.blue) .cornerRadius(25) } .padding(.top, 5) } .frame(maxWidth: .infinity, alignment: .center) } I want only the button to be clickable. How can I fix this? Thanks! Link to issue: https://imgur.com/a/iO9aNSM
1
0
70
1d
Why can't SwiftUI state be changed in the middle of view updates?
I have arrived at a certain architectural solution for my SwiftUI code which is helped by, in certain situations, modifying the state while the body is being evaluated. Of course, I am always open to realizing that a given solution may be creating difficulties precisely because it is fundamentally ill-advised. However, in this post I won't attempt to explain the details of my architecture or justify my reasoning regarding wanting to change the state in the middle of a view update. I just want to ask, why exactly is it prohibited? Is it not rather like normal recursion, which can of course produce infinite loops if done wrong but which is perfectly logically sound as long as the recursing function eventually stabilizes?
2
0
67
1d
SwiftUI view is not updated properly
Task: A child view should show depending on a boolean flag the corresponding view. The flag is calculated from a model which is given by parent view. Problem: The flag is false in init, which is correct. However in body it's true, which is incorrect. Why value in body isn't consistent to init? Is there a race condition? The child view's is rendered thrice, that's another issue, but flag is in init and body as described before. Parent view looks like this: struct ParentView: View { private var groupedItems: [GroupedItems] = [] init(items: [Item]) { Dictionary(grouping: items) { $0.category.name } .forEach { let groupedItems = GroupedItems(categoryName: $0.key, items: $0.value) self.groupedItems.append(groupedItems) } } var body: some View { ChildView(groupedItems) } } Child view looks like this: struct ChildView: View { @State private var showItems: Bool init(_ groupedItems: GroupedItems) { self._showItems = State(initialValue: !groupedItems.completed) } var body: some View { if showItems { AView() } else { BView() } } } Model looks like this: @Model final class Item: Identifiable { @Attribute(.unique) public var id: String public var name: String public var completed = false } struct GroupedItems { var categoryName: String var items: [Item] var completed: Bool { items.filter { !$0.completed }.isEmpty } }
3
0
80
1d
SwiftUI navigate to another view from a popup
when I click on a mapview pin, a PopupView comes up with some text and a button. I want to be able to navigate to another DetailsView by clicking on the button display in the popup, the button is embedded inside NavigationLink. But clicking on the button nothing happens. How to navigate from button click? struct MyMapView: View { @State var position: MapCameraPosition = .automatic @State var showCallout: Bool = false @State var selected: PinAnnotation? @Binding var locationPins: [PinAnnotation] @State private var toggler = false var body: some View { Map(position: $position) { ForEach(locationPins, id: \.self) { result in Annotation(result.title!, coordinate: result.coordinate) { ZStack { Image(systemName: "mappin.circle.fill") .resizable() .scaledToFit() .frame(width: 30, height: 30) .onTapGesture { selected = result toggler.toggle() } .foregroundStyle(.white, .purple) if selected == result && toggler { PopupView(pin: selected) } else { EmptyView() } } } } } } struct PopupView: View { @State var pin: PinAnnotation? @State private var select: Int? = 0 var body: some View { VStack (alignment: .leading) { HStack { if let val = pin { Text(val.text) .font(.system(size: 12, weight: .light, design: .default)) NavigationLink(destination: DetailsView(), label: { Button(action: {select = 1}){ Image(systemName: "play.circle") } .scaledToFit() .frame(width: 50, height: 50) .background(Color.blue) .foregroundColor(.white) .clipShape(Circle()) }) } else { Text("no data") } } // .fixedSize() } .scaledToFit() .foregroundStyle(.purple) .frame(maxWidth: .infinity) .background(Color.white) .cornerRadius(10) .shadow(radius: 5) .offset(x: 0, y: -45) } } struct DetailsView: View { @Environment(\.presentationMode) var presentation var body: some View { Group { Button("Back") { self.presentation.wrappedValue.dismiss() } } } } }
1
0
54
1d
Drawing rectangles on Layer upon object detection
Hey, I am natively an ML Engineer so not really well versed in swift. I am currently trying to use a model of mine in a swift app. The model detects knives and has an input size of 640x640. i have set up AV capture and that works well I can just see the camera on my screen. I have also set up the coreML model and it also works and detects the objects. so what doesnt work is the way I draw boxes around the object. e.g. the objects box params are Raw model output: x=342.25, y=293.0, w=105.375, h=150.5 when Im in the middle of the screen. seems ok for me, I want to draw a box now with these params. But the CGRect object is really weird to me, firstly the x value is the y value in the box (i notice this because when I move the object up and down , the x val will change but not the y val). then if I just switch the values, the drawn box will move ok in the y direction but mirrored in the x direction. what things need to be considered/changed here, is it something about the layer setup? See my implementation here: func setupLayers() { previewLayer = AVCaptureVideoPreviewLayer(session: session) previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill rootLayer = previewView.layer previewLayer.frame = rootLayer.bounds rootLayer.addSublayer(previewLayer) inferenceTimeBounds = CGRect(x: rootLayer.frame.midX-75, y: rootLayer.frame.maxY-70, width: 150, height: 17) inferenceTimeLayer = createRectLayer(inferenceTimeBounds, [1,1,1,1]) inferenceTimeLayer.cornerRadius = 7 rootLayer.addSublayer(inferenceTimeLayer) detectionLayer = CALayer() detectionLayer.frame = rootLayer.bounds detectionLayer.position = CGPoint(x: rootLayer.bounds.midX, y: rootLayer.bounds.midY) rootLayer.addSublayer(detectionLayer) }
0
0
39
1d