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

SwiftUI window top left aligned on macOS 26 beta 1
We ran into a bug with our app Bezel (https://getbezel.app). When running on macOS Tahoe, windows would get partially clipped. This is because we have SwiftUI views that are larger than the window size, our SwiftUI views are supposed to be centered, which they are on macOS 13, 14, 15. But on macOS 26 (beta 1), the window contents are top-left aligned. This seems to be a bug, I have submitted FB18201269. This is my code: WindowGroup { ZStack { Color.green ZStack { Color.yellow Text("Hi") } .aspectRatio(1, contentMode: .fill) .border(.red) } } This first screenshot shows the old behavior on macOS 15: This second screenshot shows the new behavior on macOS 26 (beta 1) Can anyone confirm if this is indeed a bug, or if this an intended change in behavior?
1
0
40
2w
button is pressed when starting scrolling in iOS 18
On iOS 18, while on a modal screen, if the scrolling starts on a button, that button gets pressed, outside of a modal this doesn't reproduce, also not reproducible on older iOS versions, neither on modals or outside of them. The code to reproduce the issue: import SwiftUI struct ContentView: View { @State var presentModal = false var body: some View { Button(action: { presentModal = true }, label: { Text("open modal") }) .sheet(isPresented: $presentModal, content: { ScrollView { ForEach(0..<100, id: \.self) { index in Button(action: { print("Button \(index) tapped!") }) { Text("Button \(index)") .frame(maxWidth: .infinity) .frame(height: 100) .background(randomColor(for: index)) .padding(.horizontal) } } } }) } func randomColor(for index: Int) -> Color { let hue = Double(index % 100) / 100.0 return Color(hue: hue, saturation: 0.8, brightness: 0.8) } } #Preview { ContentView() }
7
6
622
2w
Detect when tab bar minimizes (.tabBarMinimizeBehavior)
Hi! I'm working on a iOS 26 SwiftUI prototype that adds an element to the content of a screen only when the tab bar is fully visible and not minimized (via .tabBarMinimizeBehavior). Is there any way to detect when a tab bar is minimized? My hope is that I can use a ternary operator to display something only when a boolean is true. Here's some code to illustrate my idea: struct ContentView: View { @State var isTabBarMinimized: Bool = false var body: some View { TabView { Tab("View1", systemImage: "rainbow") { // Only appears when tab bar is fully visible Color.blue .opacity(isTabBarMinimized? 0 : 1 ) } Tab("View2", systemImage: "rainbow") { View2() } Tab("View3", systemImage: "rainbow") { View3() } Tab("View4", systemImage: "rainbow") { View4() } } .tabBarMinimizeBehavior(.onScrollDown) } }
1
0
71
2w
How to do full width .sheet() on iOS 26+ with small presentation detents?
For certain contexts, I'd like to still have my .sheet() be full width even when it's at a small height. In iOS 26, the sheet is inset from the edges at small detents and expands to full width at larger detents. For example, I have a view where I have a .sheet() that has a height of about 200pts and it contains a horizontally scrolling picker that extends past the bounds of the screen. I'd like the .sheet to expand all the way to the edge when at these small detents, like it would previous to iOS 26. Is it possible to configure this? This change will break a number of existing designs :( A new ViewModifier such as enum PresentationWidth { case dynamic, fixed } func presentationWidth(_ width: PresentationWidth) -> some View would be very nice.
1
2
51
2w
Slideshow / Gallery / Photos app View
How can I create a view that is like the one in the Photos app where you can select an item and it fills the window, then you can drag / swipe between items in that view? I have a working prototype, and it works for photos. But once I get to a video, the gesture no longer works.
2
0
37
2w
Resolving AppIntent in the app from a widget...
Is anyone familiar with AppIntents and their usage? I have an AppIntent defined in my widget extension. I have the same AppIntent defined in the app marked as ForegroundContinuableIntent. If I add the intent on a SwiftUI button and tap the Button it correctly resolves the app's version of the intent and performs that intent. However, what I really want to do is perform the AppIntent manually as the timeline updates but not more often than every hour (i.e. >= 1 hour). It's easy enough to handle the timing in the timeline. However, I do not know how to resolve the intent such that it calls the app's version of the intent (i.e. the same way that a Button does). If I just call perform it will run the version in the widget extension. The end goal is to minimally / periodically sync with the app. Thanks in advance for any help / advice on this.
Topic: UI Frameworks SubTopic: SwiftUI
3
0
109
2w
ViewThatFits and Text Truncation
I'm using ViewThatFits to handle different screen sizes as well as the orientation of the phone. Essentially, I have a smaller view that should only be used in portrait mode on the phone and a larger view that should be used in every other instance. The issue is that both of those views have a Text view that is bound to a String within a SwiftData model. If the String is too long the ViewThatFits considers that when choosing the appropriate subview. This results in a list of items where most items use one view while one or more may use the other view. It would be great if there was a modifier that could be applied to the Text view that resulted in the ViewThatFits ignoring it when determining the appropriate subview. Until such a modifier is available, has anyone come up with creative ways around this?
1
0
36
2w
Can't display image in SwiftUI
I'm trying to display my apps icon within my app and it's not working. It displays a blank space instead and I don't understand why this is happening. I tried creating a new image (just a normal image, not an 'App Icon' image set) and have this code: Image("AppIcon") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 48) .cornerRadius(10) .overlay( RoundedRectangle(cornerRadius: 10) .stroke(Color.black.opacity(0.1), lineWidth: 1) ) For some strange reason it's not displaying that either. The image name is correct. It's showing a blank white box.
2
0
53
2w
encounter memory leak for SVG image
I have a memory leak for SVG image that located in Assets.xcassets file when using SwiftUI Image, but when I use UIImage then convert it to SwiftUI Image the issue is not found. import SwiftUI struct ContentView: View { var body: some View { NavigationStack { VStack { NavigationLink("Show", destination: SecondView()) } .padding() } } } struct SecondView: View { @Environment(\.dismiss) var dismiss var body: some View { NavigationStack { VStack { IM.svgImage .resizable() .scaledToFit() .frame(width: 200, height: 200) Button("Dismiss") { dismiss() } } } } } enum IM { static let testImage: Image = "test_image".image static let svgImage: Image = "svgImage".image } extension String { var image: Image { Image(self) // Memory leak } var imageFromUIImage: Image { guard let uiImage = UIImage(named: self) else { return Image(self) } return Image(uiImage: uiImage) // No Memory leak } } Environment that produces the issue: Xcode: 16.2 Simulator: iPhone 15 Pro (iOS 17.5)
1
0
43
2w
@Transient update doesn't propagate to view
When I update a variable inside my model that is marked @Transient, my view does not update with this change. Is this normal? If I update a non-transient variable inside the model at the same time that I update the transient one, then both changes are propagated to my view. Here is an example of the model: @Model public class WaterData { public var target: Double = 3000 @Transient public var samples: [HKQuantitySample] = [] } Updating samples only does not propagate to my view.
7
8
2.6k
2w
SwiftUI lifecycle / CarPlay / data model
Is there a way to share a SwiftUI App's @Observable model into a CPTemplateApplicationSceneDelegate ? Is there an incantation to go from the UIApplicationDelegate via @UIApplicationDelegateAdaptor to the UISceneDelegate for CarPlay via a userInfo? I can't seem to figure it out. Otherwise I have to use a shared global, and I'd prefer not to have to do it this way. Any ideas? Thanks!
2
0
48
2w
PDF in WebView
Dear all, Is it possible to replace the default PDF background colour the 50% grey to any other colour while using the new WebView? Using the standard .background method on WebView does not appear to have any effect: WebView(pdfWebpage) .background(Color.blue) // no effect on the background of the PDF Thanks!
1
0
46
2w
.roundedCorners instead of .cornerRadius(?)
In WWDC 25's session Get to Know the Design system, Maria mentions that corner radius should match it's parent view or the iPhone's corners if its the outermost view. Rather than trying to figure out what number to pass into .cornerRadius(15), why not have a .roundedCorner modifier and have the system do this geometry work? See my feedback also FB17947241
Topic: UI Frameworks SubTopic: SwiftUI
2
2
84
2w