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

SwiftUI Documentation

Posts under SwiftUI subtopic

Post

Replies

Boosts

Views

Activity

A Summary of the WWDC25 Group Lab - SwiftUI
At WWDC25 we launched a new type of Lab event for the developer community - Group Labs. A Group Lab is a panel Q&A designed for a large audience of developers. Group Labs are a unique opportunity for the community to submit questions directly to a panel of Apple engineers and designers. Here are the highlights from the WWDC25 Group Lab for SwiftUI. What's your favorite new feature introduced to SwiftUI this year? The new rich text editor, a collaborative effort across multiple Apple teams. The safe area bar, simplifying the management of scroll view insets, safe areas, and overlays. NavigationLink indicator visibility control, a highly requested feature now available and back-deployed. Performance improvements to existing components (lists, scroll views, etc.) that come "for free" without requiring API adoption. Regarding performance profiling, it's recommended to use the new SwiftUI Instruments tool when you have a good understanding of your code and notice a performance drop after a specific change. This helps build a mental map between your code and the profiler's output. The "cause-and-effect graph" in the tool is particularly useful for identifying what's triggering expensive view updates, even if the issue isn't immediately apparent in your own code. My app is primarily UIKit-based, but I'm interested in adopting some newer SwiftUI-only scene types like MenuBarExtra or using SwiftUI-exclusive features. Is there a better way to bridge these worlds now? Yes, "scene bridging" makes it possible to use SwiftUI scenes from UIKit or AppKit lifecycle apps. This allows you to display purely SwiftUI scenes from your existing UIKit/AppKit code. Furthermore, you can use SwiftUI scene-specific modifiers to affect those scenes. Scene bridging is a great way to introduce SwiftUI into your apps. This also allows UIKit apps brought to Vision OS to integrate volumes and immersive spaces. It's also a great way to customize your experience with Assistive Access API. Can you please share any bad practices we should avoid when integrating Liquid Glass in our SwiftUI Apps? Avoid these common mistakes when integrating liquid glass: Overlapping Glass: Don't overlap liquid glass elements, as this can create visual artifacts. Scrolling Content Collisions: Be cautious when using liquid glass within scrolling content to prevent collisions with toolbar and navigation bar glass. Unnecessary Tinting: Resist the urge to tint the glass for branding or other purposes. Liquid glass should primarily be used to draw attention and convey meaning. Improper Grouping: Use the GlassEffectContainer to group related glass elements. This helps the system optimize rendering by limiting the search area for glass interactions. Navigation Bar Tinting: Avoid tinting navigation bars for branding, as this conflicts with the liquid glass effect. Instead, move branding colors into the content of the scroll view. This allows the color to be visible behind the glass at the top of the view, but it moves out of the way as the user scrolls, allowing the controls to revert to their standard monochrome style for better readability. Thanks for improving the performance of SwiftUI List this year. How about LazyVStack in ScrollView? Does it now also reuse the views inside the stack? Are there any best practices for improving the performance when using LazyVStack with large number of items? SwiftUI has improved scroll performance, including idle prefetching. When using LazyVStack with a large number of items, ensure your ForEach returns a static number of views. If you're returning multiple views within the ForEach, wrap them in a VStack to signal to SwiftUI that it's a single row, allowing for optimizations. Reuse is handled as an implementation detail within SwiftUI. Use the performance instrument to identify expensive views and determine how to optimize your app. If you encounter performance issues or hitches in scrolling, use the new SwiftUI Instruments tool to diagnose the problem. Implementing the new iOS 26 tab bar seems to have very low contrast when darker content is underneath, is there anything we should be doing to increase the contrast for tab bars? The new design is still in beta. If you're experiencing low contrast issues, especially with darker content underneath, please file feedback. It's generally not recommended to modify standard system components. As all apps on the platform are adopting liquid glass, feedback is crucial for tuning the experience based on a wider range of apps. Early feedback, especially regarding contrast and accessibility, is valuable for improving the system for all users. If I’m starting a new multi-platform app (iOS/iPadOS/macOS) that will heavily depend on UIKit/AppKit for the core structure and components (split, collection, table, and outline views), should I still use SwiftUI to manage the app lifecycle? Why? Even if your new multi-platform app heavily relies on UIKit/AppKit for core structure and components, it's generally recommended to still use SwiftUI to manage the app lifecycle. This sets you up for easier integration of SwiftUI components in the future and allows you to quickly adopt new SwiftUI features. Interoperability between SwiftUI and UIKit/AppKit is a core principle, with APIs to facilitate going back and forth between the two frameworks. Scene bridging allows you to bring existing SwiftUI scenes into apps that use a UIKit lifecycle, or vice versa. Think of it not as a binary choice, but as a mix of whatever you need. I’d love to know more about the matchedTransitionSource API you’ve added - is it a native way to have elements morph from a VStack to a sheet for example? What is the use case for it? The matchedTransitionSource API helps connect different views during transitions, such as when presenting popovers or other presentations from toolbar items. It's a way to link the user interaction to the presented content. For example, it can be used to visually connect an element in a VStack to a sheet. It can also be used to create a zoom effect where an element appears to enlarge, and these transitions are fully interactive, allowing users to swipe. It creates a nice, polished experience for the user. Support for this API has been added to toolbar items this year, and it was already available for standard views.
Topic: UI Frameworks SubTopic: SwiftUI
1
0
956
Jun ’25
Fix text in accessory view
Do you guys know how to fix the render of the text in the accessory view ? If I force the color of text to be .black it work but it will break dark mode, but forcing it .black : .white on color scheme changes makes white to still adapt to what is behind it I have noticed that Apple Music doesn’t have that artifact and it seems to break when images are behind the accessory view // MARK: - Next Routine Accessory @available(iOS 26.0, *) struct NetxRoutinesAccessory: View { @ObservedObject private var viewModel = RoutineProgressViewModel.shared @EnvironmentObject var colorSchemeManager: ColorSchemeManager @EnvironmentObject var routineStore: RoutineStore @EnvironmentObject var freemiumKit: FreemiumKit @ObservedObject var petsStore = PetsStore.shared @Environment(\.colorScheme) private var colorScheme // Tab accessory placement environment @Environment(\.tabViewBottomAccessoryPlacement) private var accessoryPlacement // Navigation callback var onTap: (() -> Void)? @State private var isButtonPressed = false /// Explicit black for light mode, white for dark mode private var textColor: Color { colorScheme == .dark ? .trueWhite : .trueBlack } /// Returns true when the accessory is in inline/minimized mode private var isInline: Bool { accessoryPlacement == .inline } var body: some View { accessoryContent() .onTapGesture { onTap?() } } private func accessoryContent() -> some View { HStack(spacing: 12) { // Content with smooth transitions VStack(alignment: .leading, spacing: 2) { if viewModel.totalTasks == 0 { Text(NSLocalizedString("Set up routines", comment: "Routines empty state")) .font(.subheadline.weight(.medium)) .foregroundColor(textColor) } else if let next = viewModel.nextRoutineTask() { HStack(spacing: 4) { Text(NSLocalizedString("Next", comment: "Next routine prefix")) .font(.caption) .foregroundColor(textColor) Text("•") .font(.caption) .foregroundColor(textColor) Text(next.routine.name) .font(.subheadline.weight(.medium)) .foregroundColor(textColor) .lineLimit(1) } .id("routine-\(next.routine.id)-\(next.time)") .transition(.opacity.combined(with: .move(edge: .leading))) HStack(spacing: 4) { Text(viewModel.petNames(for: next.routine.petIDs)) .font(.caption) .foregroundColor(textColor) Text("•") .font(.caption) .foregroundColor(textColor) Text(Routine.displayTimeFormatter.string(from: next.time)) .font(.caption.weight(.medium)) .foregroundColor(colorSchemeManager.accentColor ?? .blue) } .id("time-\(next.routine.id)-\(next.time)") .transition(.opacity.combined(with: .move(edge: .leading))) } else { // All tasks completed Text(NSLocalizedString("All done for today!", comment: "All routines completed")) .font(.subheadline.weight(.medium)) .foregroundColor(textColor) .transition(.opacity.combined(with: .scale)) Text("\(viewModel.completedTasks)/\(viewModel.totalTasks) " + NSLocalizedString("tasks", comment: "Tasks count suffix")) .font(.caption) .foregroundColor(textColor) } } .animation(colorSchemeManager.reduceMotion ? nil : .snappy(duration: 0.3), value: viewModel.completedTasks) .animation(colorSchemeManager.reduceMotion ? nil : .snappy(duration: 0.3), value: viewModel.progress) } .padding() .contentShape(.rect) .animation(colorSchemeManager.reduceMotion ? nil : .snappy(duration: 0.35), value: viewModel.completedTasks) } }
Topic: UI Frameworks SubTopic: SwiftUI
0
0
46
1d
Search Functionality Missing in FamilyActivityPicker on iPadOS 26.0+
Issue: The search functionality in FamilyActivityPicker has disappeared on iPadOS 26.0+. This feature was working in previous versions but is now missing. Framework: FamilyControls Expected: Search bar should be available in FamilyActivityPicker to help users find apps quickly. Actual: Search functionality is completely missing. Impact: Makes app selection difficult for users with many installed apps. Is this a known issue? If it's a bug, please address it in an upcoming update. If intentional, guidance on alternatives would be appreciated. Thank you.
0
0
10
1d
Tapping on ShareLink crashes the app
Overview Tapping on ShareLink crashes the app when ShareLink is added in the toolbar with the placement of secondaryAction Feedback FB21337385 Note: Apple engineers please priorities this is a blocker and affects production apps and prevents us from going live. Environment Xcode: 26.2 (17C52) iOS: 26.2 iPadOS: 26.2 Reproduce Able to reproduce 100% both on Simulator and Device Isolation of the crash The crash happens only when the ShareLink is used with the placement .secondaryAction The crash doesn't 'happen when the ShareLink is used with the placement .primaryAction Code import SwiftUI struct ContentView: View { var body: some View { NavigationStack { Text("Hello, world!") .toolbar { ToolbarItem(placement: .primaryAction) { Button("Dummy") { print("dummy") } } // Tapping on share button will cause it to crash // Crash only happens when the ShareLink is used with placement .secondaryAction // It doesn't crash when placement is primaryAction ToolbarItem(placement: .secondaryAction) { ShareLink(item: "Some string") } } } } } Crash stack trace *** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIActivityViewControllerPresentationController: 0x105a3b580>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.' *** First throw call stack: ( 0 CoreFoundation 0x00000001804f71d0 __exceptionPreprocess + 172 1 libobjc.A.dylib 0x000000018009c094 objc_exception_throw + 72 2 UIKitCore 0x0000000185a5b17c -[UIPopoverPresentationController presentationTransitionWillBegin] + 2712 3 UIKitCore 0x0000000185a65de0 -[UIPresentationController _presentationTransitionWillBegin] + 28 4 UIKitCore 0x0000000185a6523c __80-[UIPresentationController _initViewHierarchyForPresentationSuperview:inWindow:]_block_invoke + 1928 5 UIKitCore 0x0000000185a633ec __77-[UIPresentationController runTransitionForCurrentStateAnimated:handoffData:]_block_invoke_3 + 296 6 UIKitCore 0x00000001868b2950 -[_UIAfterCACommitBlock run] + 64 7 UIKitCore 0x00000001868b2d64 -[_UIAfterCACommitQueue flush] + 164 8 UIKitCore 0x0000000186354f04 _runAfterCACommitDeferredBlocks + 256 9 UIKitCore 0x0000000186346bec _cleanUpAfterCAFlushAndRunDeferredBlocks + 76 10 UIKitCore 0x0000000186346cb4 _UIApplicationFlushCATransaction + 68 11 UIKitCore 0x0000000186263c48 __setupUpdateSequence_block_invoke_2 + 372 12 UIKitCore 0x000000018582f378 _UIUpdateSequenceRunNext + 120 13 UIKitCore 0x00000001862640a4 schedulerStepScheduledMainSectionContinue + 56 14 UpdateCycle 0x00000002501912b4 _ZN2UC10DriverCore18continueProcessingEv + 80 15 CoreFoundation 0x000000018041a4ac __CFMachPortPerform + 164 16 CoreFoundation 0x0000000180456aa8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56 17 CoreFoundation 0x00000001804560c0 __CFRunLoopDoSource1 + 480 18 CoreFoundation 0x0000000180455188 __CFRunLoopRun + 2100 19 CoreFoundation 0x000000018044fcec _CFRunLoopRunSpecificWithOptions + 496 20 GraphicsServices 0x0000000192a669bc GSEventRunModal + 116 21 UIKitCore 0x0000000186348574 -[UIApplication _run] + 772 22 UIKitCore 0x000000018634c79c UIApplicationMain + 124 23 SwiftUI 0x00000001da58d620 $s7SwiftUI17KitRendererCommon33_ACC2C5639A7D76F611E170E831FCA491LLys5NeverOyXlXpFAESpySpys4Int8VGSgGXEfU_ + 164 24 SwiftUI 0x00000001da58d368 $s7SwiftUI6runAppys5NeverOxAA0D0RzlF + 180 25 SwiftUI 0x00000001da31b42c $s7SwiftUI3AppPAAE4mainyyFZ + 148 26 ShareLinkSecondaryPlacementDemo.deb 0x0000000104d82b0c $s31ShareLinkSecondaryPlacementDemo0abcdE3AppV5$mainyyFZ + 40 27 ShareLinkSecondaryPlacementDemo.deb 0x0000000104d82bb8 __debug_main_executable_dylib_entry_point + 12 28 dyld 0x0000000104cc53d0 start_sim + 20 29 ??? 0x0000000104ff0d54 0x0 + 4378791252 ) libc++abi: terminating due to uncaught exception of type NSException
1
0
82
2d
Capture a List selection AND push a view
I've got a situation where I want the iPad to push an editing view onto the sidebar (like on iPhone) and also display a detail view (no content view). This is working great. However, I need to determine which item from a List in a NavigationSplitView has been selected and pass this to the detail view (the selection is out of scope). This works with the List selection parameter, but the item selected is ONLY selected and the child view in the NavigationLink does not get pushed, nor does the detail view get changed. I either need to figure out a way to capture the selection without the List Selection parameter (Tap Gesture?) or get the navigation to happen even when using the Selection parameter. I'd appreciate some ideas as I'm a newb just learning SwiftUI.
Topic: UI Frameworks SubTopic: SwiftUI
0
0
34
2d
Pickers in toolbar expand its width
With iOS 26 there has been a change in behavior with Pickers in the toolbar. The Picker looks expanded unlike other views such as a Button and Menu. See screenshots below. Is this the intended behavior or a bug? (I already submitted a feedback for this at FB19276474) What Picker looks like in the toolbar: What Button looks like in the toolbar:
0
0
41
3d
SwiftUI recursive list with children: programmatically expand nodes
I have a SwiftUI recursive list, created with the (children:) initializer, just like it's shown in the code example here: https://developer.apple.com/documentation/SwiftUI/List#Creating-hierarchical-lists I would like this tree view to be searchable (i.e a user enters a query into a text field and it searches the entire tree at all levels). Displaying a search result which is not at the top level would require its parents to be programmatically expanded. How to programmatically expand certain levels of such a list?
0
0
39
3d
Preventing crashes with ScrollViewProxy.scrollTo()
I have a search field and a List of search results. As the user types in the search field, the List is updated with new results. Crucially, with each update, I want to reset the List's scroll position back to the top. To achieve this, I'm using the following .onChange() modifier along with a ScrollViewReader: .onChange(of: searchQuery) { _, newQuery in Task { searchResults = await searchLibrary(for: newQuery) scrollViewProxy.scrollTo(0, anchor: .top) } } My List uses index-based IDs, so scrolling to 0 should always go to the first item. The above code works, but crashes if searchResults is empty because there is no item in the List with an ID of 0. (As a side note, it seems rather excessive for the scrollTo() method to trigger a full-on crash just because the ID is not found; I don't think this should be anything more than a warning, or the method should throw an error that can be caught). To work around this, I added an isEmpty check, so we only attempt the scroll if the array is not empty: .onChange(of: searchQuery) { _, newQuery in Task { searchResults = await searchLibrary(for: newQuery) if !searchResults.isEmpty { scrollViewProxy.scrollTo(0, anchor: .top) } } } However, even with this check, I was seeing rare crashes in production, consistent with a race condition. My guess is that when searchResults is updated, the view is not recreated immediately, so if scrollTo() is called too quickly, the List may not yet be seeing the latest update to the searchResults array. I figured that I could try to delay the calling of scrollTo() to give the view time to update: .onChange(of: searchQuery) { _, newQuery in Task { searchResults = await searchLibrary(for: newQuery) if !searchResults.isEmpty { DispatchQueue.main.async { scrollViewProxy.scrollTo(0, anchor: .top) } } } } However, even with this, I've just received a crash report pointing to the same issue (the first in about four months). I'm not able to reproduce the bug myself – so it definitely seems like a rare race condition, probably relating to the timing of view updates. I guess, I can insert another isEmpty check before calling scrollTo(), but I'm starting to wonder if it's even possible to guarantee that the item will be in the List when scrollTo() performs its action, and because this is so hard to reproduce, I can't really test any ideas. Does anyone have any idea how (and at what point) the ScrollViewReader reads the view's current state? What's the right way to approach debugging a problem like this? Moreover, does anyone have any better suggestions about how to handle resetting the List position? The reason I want to do this is because, if the user types, scrolls a bit, and then types some more, the new results appear above the fold where the user can't see them, leading to a confusing experience. I thought about switching to the newer .scrollPosition() modifier, but that's only iOS 18+ and only for ScrollViews, not Lists. Cheers!
Topic: UI Frameworks SubTopic: SwiftUI
0
0
24
3d
SwiftData @Query causes UI Hierarchy crash
Simple code like struct ContentView: View { @Query var items: [Item] var body: some View { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .padding() } } causes UI Hierarchy crash, on Simulators, iPhone and Mac I found no solution but use @State with fetch descriptor.
0
0
30
4d
tabBarMinimizeBehavior behavior in iOS 26
I'm trying to revamp the player into a floating style like Apple music. I use tabViewBottomAccessory with tabBarMinimizeBehavior. At the time, I noticed an issue that tabViewBottomAccessory would not automatically collapse when the scroll area was small (but still exceeded the screen height). tabViewBottomAccessory can only be automatically collapsed when the scroll area is large enough. Below is the simplest demo. I'm not sure if it's intentional or if it's a bug. Besides, I wonder if we can control it programmatically(expanded/inline)? struct ContentView: View { var body: some View { TabView { Tab("Numbers", systemImage: "number.circle") { List { // 200 works well, but 20 not ForEach(0..<200) { index in Text("\(index)") } } } } .tabBarMinimizeBehavior(.onScrollDown) .tabViewBottomAccessory { HStack { Text("SwiftUI Demo App") } } } }
1
0
108
4d
How to improve my SwiftUI tvOS app flow?
Hello, I'm thinking about how to improve my main tvOS app flow, naively I want to do something like this: import Combine import SwiftUI enum AppState { case login, onboarding, main } class AppStateManager { let appStatePublisher = PassthroughSubject<AppState, Never>() func updateState(_ appState: AppState) } struct tvOSApp: App { private var appState: AppState = .login private let appStateManager = AppStateManager() var body: some Scene { WindowGroup { ZStack { switch appState { case .login: LoginView() case .onboarding: OnboardingView() case .main: MainView() } } .onReceive(appStateManager.appStatePublisher) { self.appState = $0 } } } } So basically, MainView, OnboardingView and LoginView would be the main navigation views of my app, and the appStateManager would be a dependency passed to each of these views and allowing me to update the currently displayed view in the app. (of course I could use an Environment object instead for a 100% SwiftUI solution). I was wondering, however, if there is a better way to do this, instead of switching in a ZStack, maybe with WindowGroup/Window/Scenes? Thank you for your help!
0
0
39
5d
How to disable highlight state on Link in LA widget
I am working on a Live Activity widget. In it, I want some of the elements to open different deeplink URLs. I have found that assigning multiple widgetURL doesn't work, only one of the URLs gets opened no matter where you tap. I also found that Buttons don't seem to do anything, tapping them actually just open my app as if I just tapped a naked Live Activity. I have found that really only Link elements work if I want to open different URLs upon tapping different elements. And Links are cool and fine, but I am seeing that on tap, my elements become tinted... As in, there is a highlighted state, and it makes the elements inside blue. I have tried to use button style API on a link, but it didn't work. How can I disable the highlighted state for a Link element in a live activity widget?
0
0
45
5d
Delete Confirmation Dialog Inside Toolbar IOS26
inline-code How do you achieve this effect in toolbar? Where is the documentation for this? How to make it appear top toolbar or bottom toolbar? Thank you! Here is what I have now... .toolbar { ToolbarItem(placement: .destructiveAction) { Button { showConfirm = true } label: { Image(systemName: "trash") .foregroundColor(.red) } } } .confirmationDialog( "Are you sure?", isPresented: $showConfirm, titleVisibility: .visible ) { Button("Delete Item", role: .destructive) { print("Deleted") } Button("Archive", role: .none) { print("Archived") } Button("Cancel", role: .cancel) { } } }
0
0
29
6d
How do I prevent screenshots using SwiftUI?
Hi Team, How do I prevent screenshots using SwiftUI. I was using this solution on UIKit: extension UIView { func makeSecure() { DispatchQueue.main.async { let protectedView = UIView() self.superview?.addSubview(protectedView) // constraints... let secureView = SecureView() self.superview?.addSubview(secureView) // constraints... secureView.addSecureSubview(self) // constraints... } } } class SecureView: UIView { private lazy var secureField: UIView = { var secureField: UIView = UIView() // ... if let secureContainer = SecureField().secureContainer { secureField = secureContainer } ... return secureField }() required init() { ... } } Is it posible to do the same thing using SwiftUI. Do we have an example? What would you recommend when we work with confidencial information in SwiftUI like bank account information? Thanks in advance!
0
0
115
6d
Zoom navigation transitions for tabViewBottomAccessory are not working in SwiftUI with ObservableObject or Observable
The zoom navigation transition with matchedTransitionSource in tabViewBottomAccessory does not work when a Published var in an ObservableObjector Observable gets changed. Here is an minimal reproducible example with ObservableObject: import SwiftUI import Combine private final class ViewModel: ObservableObject { @Published var isPresented = false } struct ContentView: View { @Namespace private var namespace @StateObject private var viewModel = ViewModel() // @State private var isPresented = false var body: some View { TabView { Button { viewModel.isPresented = true } label: { Text("Start") } .tabItem { Image(systemName: "house") Text("Home") } Text("Search") .tabItem { Image(systemName: "magnifyingglass") Text("Search") } Text("Profile") .tabItem { Image(systemName: "person") Text("Profile") } } .sheet(isPresented: $viewModel.isPresented) { Text("Sheet") .presentationDragIndicator(.visible) .navigationTransition(.zoom(sourceID: "tabViewBottomAccessoryTransition", in: namespace)) } .tabViewBottomAccessory { Button { viewModel.isPresented = true } label: { Text("BottomAccessory") } .matchedTransitionSource(id: "tabViewBottomAccessoryTransition", in: namespace) } } } However, when using only a State property everything works: import SwiftUI import Combine private final class ViewModel: ObservableObject { @Published var isPresented = false } struct ContentView: View { @Namespace private var namespace // @StateObject private var viewModel = ViewModel() @State private var isPresented = false var body: some View { TabView { Button { isPresented = true } label: { Text("Start") } .tabItem { Image(systemName: "house") Text("Home") } Text("Search") .tabItem { Image(systemName: "magnifyingglass") Text("Search") } Text("Profile") .tabItem { Image(systemName: "person") Text("Profile") } } .sheet(isPresented: $isPresented) { Text("Sheet") .presentationDragIndicator(.visible) .navigationTransition(.zoom(sourceID: "tabViewBottomAccessoryTransition", in: namespace)) } .tabViewBottomAccessory { Button { isPresented = true } label: { Text("BottomAccessory") } .matchedTransitionSource(id: "tabViewBottomAccessoryTransition", in: namespace) } } }
0
0
64
6d
@state update not reflecting on UI.
I’m facing an issue in our native iOS app that occurs specifically on iOS 26.1 (not observed on any lower versions). When I update a @State field value, the UI does not reflect the change as expected. The @State variable updates internally, but the view does not re-render. This behaviour started after upgrading to iOS 26.1. Works fine on iOS 26.0 and earlier versions. Has anyone else encountered this issue or found a workaround? Any insights or suggestions would be greatly appreciated.
5
0
209
4d
SwiftUI: NavigationSplitView + Toolbar + Button = Constraint Warning
As the title says, when I try to add a Toolbar with a Button to my NavigationSplitView I get a warning about satisfying constraints. Here is a minimal reproducible example: import SwiftUI @main struct ViewTestingApp: App { var body: some Scene { WindowGroup { NavigationSplitView { Text("Sidebar") .toolbar { ToolbarItem(placement: .topBarTrailing) { Button { debugPrint("Hello World!") } label: { Label("", systemImage: "flame") } } } } content: { Text("Content") } detail: { Text("Detail") } } } } This is the specific warning I get: Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600002164960 h=--& v=--& _TtCC5UIKit19NavigationButtonBar15ItemWrapperView:0x100f80fa0.width == 0 (active)>", "<NSLayoutConstraint:0x600002160370 _TtCC5UIKit19NavigationButtonBar15ItemWrapperView:0x100f80fa0.leading == _UIButtonBarButton:0x100f7d360.leading (active)>", "<NSLayoutConstraint:0x6000021603c0 H:[_UIButtonBarButton:0x100f7d360]-(0)-| (active, names: '|':_TtCC5UIKit19NavigationButtonBar15ItemWrapperView:0x100f80fa0 )>", "<NSLayoutConstraint:0x600002160050 'IB_Leading_Leading' H:|-(2)-[_UIModernBarButton:0x100f7e6c0] (active, names: '|':_UIButtonBarButton:0x100f7d360 )>", "<NSLayoutConstraint:0x6000021600a0 'IB_Trailing_Trailing' H:[_UIModernBarButton:0x100f7e6c0]-(2)-| (active, names: '|':_UIButtonBarButton:0x100f7d360 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000021600a0 'IB_Trailing_Trailing' H:[_UIModernBarButton:0x100f7e6c0]-(2)-| (active, names: '|':_UIButtonBarButton:0x100f7d360 )> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. The project settings are all at their defaults, but in case anyone wants to try it with the whole project: https://github.com/OddMagnet/ViewTesting
Topic: UI Frameworks SubTopic: SwiftUI
3
0
94
6d
SwiftUI Map overlay z-order: make MapPolyline consistently render above MapPolygon (aboveLabels)
Hi, On a SwiftUI map I render a mix of MapPolygon and MapPolyline. All overlays must use the same overlay level (.aboveLabels). Goal: Ensure MapPolyline always renders on top of MapPolygon. Issue: I order data so polylines are last and even render in two passes (polygons first, polylines second), all at .aboveLabels. Despite that, after polygons change (items removed/added based on zoom levels), I see polygons visually on top of polylines. It seems MapKit may batch/reorder rendering internally. Questions: Is there a reliable way in SwiftUI Map to enforce z-order within the same overlay level so MapPolyline always appears above MapPolygon? If not, any known workarounds or best practices? (e.g. different composition patterns, using annotations with zIndex, or other techniques compatible with SwiftUI Map) I know you can do this with UIKit, but first looking for a solution compatible with SwiftUI's version of MapKit. Thanks
0
0
49
1w
SwiftUI - presentationDetents behaves incorrectly on iOS 16–18 but works correctly on iOS 26
I'm using a custom modifier called AutoSheetDetentModifier to automatically size a sheet based on its content. On iOS 26, it works as expected: the content height is measured correctly and the sheet shrinks to match that height. However, on iOS 16, 17 and 18, the same code doesn’t work. The content height is still measured, but the sheet does not reduce its height. Instead, the sheet remains larger and the content appears vertically centered. (Note that content() includes ScrollView) public struct AutoSheetDetentModifier: ViewModifier { @State private var height: CGFloat = 380 // default value to avoid bouncing public func body(content: Content) -> some View { content .modifier(MeasureHeightViewModifier(height: $height)) .presentationDetents([.height(height)]) } } public struct MeasureHeightViewModifier: ViewModifier { @Binding var height: CGFloat public func body(content: Content) -> some View { content .fixedSize(horizontal: false, vertical: true) .background( GeometryReader { geo -> Color in DispatchQueue.main.async { height = geo.size.height } return Color.clear } ) } } extension View { public func applyAutoSheetDetent() -> some View { self .modifier(AutoSheetDetentModifier()) } } public var body: some View { VStack { header() content() // includes ScrollView footer() } .background(Color.customGray) .applyAutoSheetDetent() } func content() -> some View { ScrollView { VStack { ForEach(items) { item in itemRow(item) } } } .frame(maxHeight: UIScreen.main.bounds.height * 0.7) } Screenshot from iOS 26 (working as expected): Screenshot from iOS 18 (not working): How can I make .presentationDetents(.height) shrink the sheet correctly on iOS 16–18, the same way it does on iOS 26?
1
0
100
1w