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

SwiftUI Documentation

Post

Replies

Boosts

Views

Activity

Swiftui Bug - Button (only top part is clickable)
I'm having an issue with SwiftUI where only the top part of the button is clickable, while the bottom part is not clickable at all. Here is my code snippet. Does anyone know how I can fix this? var body: some View { VStack(spacing: 0) { ScrollView(.horizontal) { HStack { Button(action: {}, label: { ZStack { Color.green Text("Button") .padding(8) .contentShape(Rectangle()) } }) } } .background(Color.blue) .frame(height: 40) TabView { ZStack { Color.yellow Text("my view") } } } } }
1
0
207
Sep ’24
Hiding the tabBar
How do I hide the tabBar for certain navigations? I am aware of .toolbar(.hidden, for: .tabBar) but it has issues. Here is the effect I actually want (screenshot is of transitioning from one page to another) The .toolbar modifier doesn't work like that. Once the other page has been navigated to, it then shows the tabBar. That isn't what I want: I want the toolbar to be visible on the other page while swiping from page to page. I am on iOS 18/Sequoia. This is my code: NavigationStack { List { NavigationLink("your bday plans") { Text("birthday email!") .toolbar(.hidden, for: .tabBar) } } .navigationTitle("Mail") }
2
1
228
Sep ’24
Why does dismiss() have no impact inside a closure of NavigationDestination view?
In the code below, for the #else case, when I click Click to dismiss, the button remains. But for the #if DISMISS_SUCCESSFULLY case, clicking the button causes pops it of the navigation stack. Would you please help explain why the difference? Thank you. Button("Click to navigate") { myNavigate = true } .navigationDestination(isPresented: $myNavigate) { #if DISMISS_SUCCESSFULLY DismissButton() #else Button("Click it dismiss") { print("Dismissing ...") presentationMode.dismiss() print("Done dismissing") } #endif } struct DismissButton: View { //=== Local === @Environment(\.presentationMode) @Binding private var presentationMode var body: some View { Button("Click it dismiss") { presentationMode.dismiss() } } }
2
0
201
Sep ’24
iOS 18 beta - .onAppear in NavSplitView creates an infinite loop
Bug in iOS 18 beta? .onAppear of a NavSplitView-DetailView is called repeatedly after dismissing the view. The app gets unresponsive and can only be killed and cold-restartet. What happens: I update a Binding property in .onAppear of a NavigationSplitView-DetailView. Whenever I leave the DetailView going back to the ListView (the NavSplitView’s first column), the DetailView's .onAppear get’s triggered in an endless loop. This blocks the main thread and freezes the app. This behaviour is only appearing in iOS 18 betas (simulators and devices) and not on iOS 17.x releases. If I remove the code updating the Binding, the .onAppear loop does not happen. Another way of triggering the same unexpected behaviour is by updating an OberservedObject appState.shared singleton, e.g. by a timer. Has anybody come across this type of behaviour? Is this a know change of iOS 18 behaviour or a bug even?
2
0
243
Sep ’24
Swift Charts Crashing Xcode 16.1 Beta
Feedback FB14988865 Looks like in the latest version of Xcode 16.1 SwiftCharts crashes on launch. Below are the crashlogs. dyld[88826]: Symbol not found: _$s6Charts12BuilderTupleVyxxQp_QPGAA8AxisMarkAARvzAaERzlMc Referenced from: <01BB785A-84AF-3689-A614-DBFEB6A9733F> /Users/xxxx/Library/Developer/CoreSimulator/Devices/A6841249-F73B-45F2-AB68-96F94D75ACF7/data/Containers/Bundle/Application/E88B6681-E933-48AC-920A-150106F12A1F/xxxxx/xxxxx.debug.dylib Expected in: <35624EEC-5BA2-3545-B05D-BABFE6661F1B> /Library/Developer/CoreSimulator/Volumes/iOS_22A5326g/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 18.0.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Charts.framework/Charts It looks related to using AxisMarks to create custom axis.
1
1
308
Aug ’24
focusedSceneValue doesn't work with Window
My observations: Passing a FocusState.Binding using focusedSceneValue seems to work only when the app uses a WindowGroup. When the app uses a Window (single window) passing the FocusState.Binding in focusedSceneValue doesn't seem to work. When app uses Window passing FocusState.Binding using focusedValue and receiving it in @FocusedValue seems to work. Question: Is this expected behaviour? (should focusedSceneValue only work for WindowGroup and not for Window?)
0
0
213
Aug ’24
Ignore accented rendering mode?
In iOS18 the user can change their Home Screen customization to choose either light, dark, or tinted. If they choose tinted the widgets are rendered using the "accented" rendering mode and without a background. Is there some way to override so that the tinted mode is ignore completely and the widgets render as full color? I know about WidgetAccentedRenderingMode (https://developer.apple.com/documentation/widgetkit/widgetaccentedrenderingmode) but that's only for images, not the whole control and doesn't help with the background also being removed in the tinted mode.
1
0
265
Aug ’24
Problems with navigation stack inside a tab view
So basically, if I put a .navigationModifier inside of a NavigationStack that's inside of a TabView, AND I supply a path argument to NavigationStack. I get this error: Do not put a navigation destination modifier inside a "lazy” container, like `List` or `LazyVStack`. These containers create child views only when needed to render on screen. Add the navigation destination modifier outside these containers so that the navigation stack can always see the destination. There's a misplaced `navigationDestination(for:destination:)` modifier for type `NavPath`. It will be ignored in a future release. Does TabView or NavigationStack suddenly become lazy when I put the path parameter in? It also causes weird unexplained behavior if I start removing tabs using state, but I do think I'm not supposed to do that. Code for reference: var body: some View { @Bindable var nm = navManager TabView{ NavigationStack(path: $nm.pathStack){ HomeView() // A custom view .navigationDestination(for: NavPath.self) { np in // NavPath is a custom struct that's hashable switch np.pathId { case "home": NavigationLazyView(HomeView()) // Ignore the NavigationLazyView, does not affect ...
3
1
283
Aug ’24
Migrating SwiftUI lifecycle to UIKit's
Hi, everyone I have an app already in production that uses SwiftUI's lifecycle (paired with an AppDelegate). Due to some specific behaviour of the app, we decided to migrate the app to use UIKit's lifecycle, adding the corresponding SceneDelegate to the app, as well as modifying the Info.plist file accordingly to accommodate to these new changes. Although everything seems to work when installing the app from zero, when installing it on top of another version, the screen goes black and the user cannot interact with the app at all unless they reinstall it completely. As I've read online, iOS is reusing the window configuration from the previous execution of the app. I know this because the AppDelegate's application(application:connectingSceneSession:options) is not being called when coming from a previous version of the app. I would love to know what can I do to make this work because, as you may understand, we cannot ask our user base to reinstall the application. Thank you very much.
5
2
357
Aug ’24
Image aspect ratio
I want to maintain a 1/1 square aspect ratio (equal width and height) without specifying an exact height/width. This is proving unreasonably hard. My code is below. Image(.personsmiling) .resizable() .aspectRatio(1/1, contentMode: .fill) .padding() Previously I have used scaledToFill() and clipped() but that approach doesn't work here
4
0
183
Aug ’24
.onMove drag and drop
Hi everyone, I’m working on a SwiftUI project where I want the entire page to be scrollable. Within this page, I have a bottom section that displays a list of items, and these items are being rendered from a SwiftData model object. My main goal is to make this list reorderable using the onMove method. The challenge I’m facing is that using a List inside a ScrollView requires a fixed height for the List, which doesn’t work for my use case because the content is dynamic and the height can vary. Since I’m using SwiftData to manage my data, I need a way to keep this list fully dynamic. I’m open to using a VStack instead of a List to display the items, but I still need the reorder functionality that List provides. Does anyone know a good way to achieve this behavior? Is there a way to use onMove or similar functionality without needing a fixed height, or perhaps an alternative approach that might work better with SwiftData? Any suggestions or insights would be greatly appreciated! Thanks!
2
0
232
Aug ’24
Too Fast Drag on visionOS
There is an issue in visionOS with a drag moving too fast when using .draggable or .onDrag. Below is a simple example. Here is a circle view and a rectangle view. The goal is to drag the circle to the rectangle. The issue is that when starting the drag the circle moves very fast with momentum and stops far to the right of the rectangle. On platforms other than visionOS, the drag is smooth and does not overshoot. Is there a way to control the drag on visionOS? Here is example code: struct TooFastDragView: View { var body: some View { HStack { Image(systemName: "circle") .resizable() .scaledToFit() .frame(width: 60, height: 60) .padding(0) .draggable("circle") Rectangle() .foregroundStyle(.secondary.opacity(0.3)) .dropDestination(for: String.self) { items, location in return true } .frame(width: 60, height: 60) } .padding() } }
2
0
196
Aug ’24
Showing .subscriptionPromotionalOffer() does not render any available offers if user already subscribed
Hi, I working with the new .subscriptionPromotionalOffer() modifier of SubscriptionStoreView, with the goal of presenting available promotional offers to users that have or have not yet subscribed to one of my subscriptions. In the former case, the promotional offers are rendered correctly along with the relevant subscription plan. But in the latter, if the user has already subscribed to the subscription, the promotional offers are not rendered at all, and the UI displays a grayed out "Current Plan" button. I expect the user to be able to enjoy this promotional offer, even it he's subscribed to the plan and he hasn't been offered the offer before. Why is it not showing for this user? Should not existing promotional offers be available to existing subscribers? struct SubscriptionsView: View { var subscriptions: [String] var offerId: String? var body: some View { SubscriptionStoreView(productIDs: subscriptions) { // UI content } .subscriptionPromotionalOffer( offer: { product, subscriptionInfo in return subscriptionInfo.promotionalOffers.first(where: { $0.id == offerId }) }, signature: { product, subscriptionInfo, promotionalOffer in return try await store.getSignature(product: product,subscriptionInfo: subscriptionInfo, promotionalOffer: promotionalOffer ) }) } }
1
0
240
Aug ’24
Duplicate bar buttons appear when .toolbar is applied to a Group View
Is it normal behaviour that when I apply a .toolbar to a Group that I see duplicate buttons equal to the number of Views in the Group? e.g. Group { Text("1") Text("2") Text("3") } .toolbar { Button("Hi") { } } Results in 3 toolbar buttons appearing in the UI, like in this screenshot: If not, then I'll submit feedback about this bug but thought I'd ask first. Xcode Version 16.0 beta 6 (16A5230g) iPhone 15 Pro Simulator
3
0
274
Aug ’24
Control Widgets vs. Interactive Widgets
What advantages do Control Widgets offer over Interactive Widgets, besides their ability to be added to Control Center, Lock Screen, and the Action Button? As far as I understand, both types of widgets provide toggle and button actions to deep link the widget to key functions within the main app.
1
0
241
Aug ’24