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

Posts under SwiftUI tag

200 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

How do I properly mix SwiftUI Views with Auto Layout Constraint animations?
I have a SwiftUI View I've introduced to a UIKit app, using UIHostingController. The UIView instance that contains the SwiftUI view is animated using auto layout constraints. In this code block, when a view controller's viewDidAppear method I'm creating the hosting controller and adding its view as a subview of this view controller's view, in addition to doing the Container View Controller dance. override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let hostingViewController = UIHostingController(rootView: TestView()) hostingViewController.view.translatesAutoresizingMaskIntoConstraints = false addChild(hostingViewController) view.addSubview(hostingViewController.view) let centerXConstraint = hostingViewController.view.centerXAnchor.constraint(equalTo: view.centerXAnchor) let topConstraint = hostingViewController.view.topAnchor.constraint(equalTo: view.topAnchor) widthConstraint = hostingViewController.view.widthAnchor.constraint(equalToConstant: 361) heightConstraint = hostingViewController.view.heightAnchor.constraint(equalToConstant: 342) NSLayoutConstraint.activate([centerXConstraint, topConstraint, widthConstraint, heightConstraint]) hostingViewController.didMove(toParent: self) self.hostingViewController = hostingViewController } I add a button to the UI which will scale the UIHostingViewController by adjusting its height and width constraints. When it's tapped, this action method runs. @IBAction func animate(_ sender: Any) { widthConstraint.constant = 120.3 heightConstraint.constant = 114.0 UIView.animate(withDuration: 0.5) { self.view.layoutIfNeeded() } } The problem is, the SwiftUI view's contents "jump" at the start of the animation to the final height, then animate into place. I see this both using UIView.animate the UIKit way, or creating a SwiftUI animation and calling `UIView. What else do I need to add to make this animate smoothly?
1
0
63
3h
Is it possible to automatically flip device orientation?
Hello togehter, i do have the following question. If I have my App run in landscape mode and a sheet view get's called, will it be possible to switch automatically from landscape mode in portrait mode and fix this device orientation? Once the sheet view get's dismissed or closed, the original view will come back and the device orientation shall switch back to landscape mode. Thanks you so much for your help!
0
0
54
4h
iPad screen size
Hi, Does the iPad Playgrounds app act completely the same way as a MacBook Playground? I am developing my app on a 2020 MacBook Air M1 using Swift Playgrounds. However, since the testing is going to be done on an iPad Swift Playgrounds, I was worried if my playground would work, since it relies heavily on the screen size etc. My app runs completely perfect on MacBook Playrgounds, but doesn't work on the iPad simulator on Xcode.
0
0
80
9h
SwiftData crash when using a @Query on macOS 15.3.x
We use @Query macro in our App. After we got macOS 15.3 update, our App crashes at @Query line. SwiftData/Schema.swift:305: Fatal error: KeyPath \Item.<computed 0x0000000100599e54 (Vec3D)>.x points to a field (<computed 0x0000000100599e54 (Vec3D)>) that is unknown to Item and cannot be used. This problem occurs only when the build configuration is "Release", and only when I use @Query macro with sort: parameter. The App still works fine on macOS 14.7.3. This issue seems similar to what has already been reported in the forum. It looks like a regression on iOS 18.3. https://developer.apple.com/forums/thread/773308 Item.swift import Foundation import SwiftData public struct Vec3D { let x,y,z: Int } extension Vec3D: Codable { } @Model final class Item { var timestamp: Date var vec: Vec3D init(timestamp: Date) { self.timestamp = timestamp self.vec = Vec3D(x: 0, y: 0, z: 0) } } ContentView.Swift import SwiftUI import SwiftData struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query(sort: \Item.vec.x) // Crash private var items: [Item] var body: some View { NavigationSplitView { List { ForEach(items) { item in NavigationLink { Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))") } label: { Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard)) } } .onDelete(perform: deleteItems) } .navigationSplitViewColumnWidth(min: 180, ideal: 200) .toolbar { ToolbarItem { Button(action: addItem) { Label("Add Item", systemImage: "plus") } } } } detail: { Text("Select an item") } } private func addItem() { withAnimation { let newItem = Item(timestamp: Date()) modelContext.insert(newItem) } } private func deleteItems(offsets: IndexSet) { withAnimation { for index in offsets { modelContext.delete(items[index]) } } } }
0
0
52
11h
@State memory leak Xcode 16.2
Hi, A class initialized as the initial value of an @State property is not released until the whole View disappears. Every subsequent instance deinitializes properly. Am I missing something, or is this a known issue? struct ContentView: View { // 1 - init first SimpleClass instance @State var simpleClass: SimpleClass? = SimpleClass(name: "First") var body: some View { VStack { Text("Hello, world!") } .task { try? await Task.sleep(for: .seconds(2)) // 2 - init second SimpleClass instance and set as new @State // "First" should deinit simpleClass = SimpleClass(name: "Second") // 3 - "Second" deinit just fine simpleClass = nil } } } class SimpleClass { let name: String init(name: String) { print("init: \(name)") self.name = name } deinit { print("deinit: \(name)") } } output: init: First init: Second deinit: Second Thanks
0
0
91
18h
A symbol on Apple Maps, how can I show it in MapKit
The easiest way to explain this is to show it. On any device, open Maps, set it to Driving (which will show traffic). Go to Baltimore Maryland. In the water just south east of the city there is a bridge (Francis Scott Key Bridge). . On Apple Maps the road is colored dark red. At certain zoom levels, there is a "button" (red circle with a white - in it). When you click on that "button", it says 1 Advisory (Road Closed). How do I show this "button" on my map. My map shows the dark red color, but no "button" appears. The only "advisory" that I've been able to find is when you create a route. Of course you can't create a route over a road that fell into the water. struct ContentView: View { @State private var position = MapCameraPosition.region( MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 39.22742855118304, longitude: -76.52228412310761), span: MKCoordinateSpan(latitudeDelta: 0.05407607689684113, longitudeDelta: 0.04606660133347873) ) ) var body: some View { Map(position: $position) .mapStyle(.standard(pointsOfInterest: .all, showsTraffic: true)) .cornerRadius(25) } } Is this a WCDWAD, or is there a way to show the "button" (We Can't Do What Apple Does)
5
0
126
2h
XCode Launch Screen File wont save in project settings
Hey everyone! Totally a newbe question here but cant find the answer anywhere when searching so figured I would just post. Trying to create my first SwiftUI app in Xcode and wanted to create a simple launch screen with an image file, and a text label. however when I select the launch screen in the Project settings it lets me pick it, but then when i navigate away it never saves the value. Then when I go somewhere else and come back its blank again... Any thoughts? XCode 16.2
2
0
115
1d
SwiftUI Vs AppKit for a complex and dynamic UI
I'm looking to develop a very rich networking macOS app (like social media apps) operated by very large number of users, each user is able to create a number of windows, operate/view each of them, able to customize the app to his liking etc. The UI is expected to be very rich and dynamic. The question is, should I choose AppKit or SwiftUI? I have a basic understanding of SwiftUI, its declarative way of defining UI layouts and populating it with data. Not sure if SwiftUI can handle a very rich and dynamic UI customised by large number of users. Any thoughts? What works best in this scenario? What is Apple's recommendation?
1
1
142
1d
App Launches on Login but Window Doesn't Appear Automatically (macOS Sequoia 15.2, Xcode 16.2)
I am developing a macOS app using SwiftUI, and I am encountering an issue when launching the app at login. The app starts as expected, but the window does not appear automatically. Instead, it remains in the Dock, and the user must manually click the app icon to make the window appear. Additionally, I noticed that the timestamp obtained during the app's initialization (init) differs from the timestamp obtained in .onAppear. This suggests that .onAppear does not trigger until the user interacts with the app. However, I want .onAppear to execute automatically upon login. Steps to Reproduce Build the app and add it to System Settings > General > Login Items as an item that opens at login. Quit the app and restart the Mac. Log in to macOS. Observe that the app starts and appears in the Dock but does not create a window. Click the app icon in the Dock, and only then does the window appear. Expected Behavior The window should be created and appear automatically upon login without requiring user interaction. .onAppear should execute immediately when the app starts at login. Observed Behavior The app launches and is present in the Dock, but the window does not appear. .onAppear does not execute until the user manually clicks the app icon. A discrepancy exists between the timestamps obtained in init and .onAppear. Sample Code Here is a minimal example that reproduces the issue: LoginTestApp.swift import SwiftUI @main struct LoginTestApp: App { @State var date2: Date init(){ date2 = Date() } var body: some Scene { WindowGroup { MainView(date2: $date2) } } } MainView.swift import SwiftUI struct MainView: View { @State var date1: Date? @Binding var date2: Date var body: some View { Text("This is MainView") Text("MainView created: \(date1?.description ?? "")") .onAppear { date1 = Date() } Text("App initialized: \(date2.description)") } } Test Environment Book Pro 13-inch, M1, 2020 macOS Sequoia 15.2 Xcode 16.2 Questions Is this expected behavior in macOS Sequoia 15.2? How can I ensure that .onAppear executes automatically upon login? Is there an alternative approach to ensure the window is displayed without user interaction?
0
0
108
1d
System issues encountered when developing VisionOS related programs using Unity
Using Unity to develop VisionOS program, pressing the right knob of VisionPro during use will exit the Unity space and destroy the model in the space. The model in the space has been disconnected from the SwiftUI interface. After clicking the right knob, return to the system main interface, and then click the right knob again to return to the inside of the program. However, Unity space cannot be restored, and calling the discisWindow method on the SwiftUI interface has no effect, so the interface cannot be destroyed. Is there any solution??
0
0
105
1d
Is using to SwiftUI entry point and using NSViewControllerRepresentable to add NSViewController to hirarchy right approach ?
In macOS application, we are using SwiftUI as an entry point to our application and attaching appdelegate using NSApplicationDelegateAdaptor. We are using NSViewControllerRepresentable to add a View Controller to the hiracrchy so that we can store intance of viewcontroller and add content to it programatically . @main struct TWMainApp: App { @NSApplicationDelegateAdaptor private var appDelegate: TWAppDelegate internal var body : some Scene { TWInitialScene () } } TWInitialScene : public struct TWInitialScene : Scene { public var body : some Scene { WindowGroup { TWInitialView () } } } TWInitialView : struct TWInitialView : View { @Environment(\.scenePhase) private var scenePhase var body : some View { TWAppKitToSwiftUIBridge () } } TWAppKitToSwiftUIBridge : struct TWNSKitToSwiftUIBridge : NSViewControllerRepresentable { func makeNSViewController(context: Context) -> TWNSViewController { let view_hierarchy : TWNSViewController view_hierarchy = TWStaticContext.sViewController return view_hierarchy } func updateNSViewController(_ nsViewController: TWNSViewController, context: Context) { } } @objc public class TWStaticContext : NSObject { public static let sViewController = TWNSViewController () public override init () {} @objc public static func GetViewController () -> TWNSViewController { return TWStaticContext.sViewController } } public class TWNSViewController : NSViewController { override public func viewDidLoad () { super.viewDidLoad () } } To add content to the hirarchy we are accessing viewcontroller's intance and adding content to it like this : public func PaintInitialScreen () { let label = NSTextField(labelWithString: "TW window") label.frame = NSRect(x: 100, y: 200, width: 200, height: 200) // Adding content to viewcontroller TWStaticContext.sViewController.view.addSubview(label) } We are using this approach because we have a contraint in our application that we have to update UI programatically and on compile time we dont know what we want to show . We will be adding content on runtime based on how many button we want, what label we want , where to place it etc. When we were using purely appKit application, doing things programatically was simple but since SwiftUI is a declarative application we have to use above approach. Rational for shifting to SwiftUI entry point is that we want our application to be future safe and since apple is more inclined to SwiffUI, we want to design our entry flow to use SwiftUI entry point . And SwiftUI being declarative, we are using appKit to add content to hiracrchy programtically. We have used similar apprach in iOS also , where are using UIApplicationDelegateAdaptor inplace of NSApplicationAdaptor . And UIViewControllerReprestable in place of NSViewControllerRepresentable. Is this right approach to use ?
1
0
168
1d
Optimal Way of Getting Screen Resolution Using SwiftUI
Hi all, I am looking for a futureproof way of getting the Screen Resolution of my display device using SwiftUI in MacOS. I understand that it can't really be done to the fullest extent, meaning that the closest API we have is the GeometeryProxy and that would only result in the resolution of the parent view, which in the MacOS case would not give us the display's screen resolution. The only viable option I am left with is NSScreen.frame. However, my issue here is that it seems like Apple is moving towards SwiftUI aggressively, and in order to futureproof my application I need to not rely on AppKit methods as much. Hence, my question: Is there a way to get the Screen Resolution of a Display using SwiftUI that Apple itself recommends? If not, then can I rely safely on NSScreen's frame API?
2
0
79
1d
Moving SceneDelegate to a different target
I have a SwiftUI project which has the following hierarchy: IOSSceneDelegate (App target) - depends on EntryPoint and Presentation static libs. Presentation (Static library) - Depends on EntryPoint static lib. Contains UI related logic and updates the UI after querying the data layer. EntryPoint (Static library) - Contains the entry point, AppDelegate (for its lifecycle aspects) etc. I've only listed the relevant targets here. SceneDelegate was initially present in EntryPoint library, because the AppDelegate references it when a scene is created. public func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -&gt; UISceneConfiguration { // Set the SceneDelegate dynamically let sceneConfig: UISceneConfiguration = UISceneConfiguration(name: "mainWindow", sessionRole: connectingSceneSession.role) sceneConfig.delegateClass = SceneDelegate.self return sceneConfig } The intent is to move the SceneDelegate to the Presentation library. When moved, the EntryPoint library fails to compile because it's referencing the SceneDelegate (as shown above). To remove this reference, I tried to set up the SceneDelegate in the old way - In the info.plist file, mention a SceneConfiguration and set the SceneDelegate in Presentation. // In the Info.plist file &lt;key&gt;UIApplicationSceneManifest&lt;/key&gt; &lt;dict&gt; &lt;key&gt;UIApplicationSupportsMultipleScenes&lt;/key&gt; &lt;true/&gt; &lt;key&gt;UISceneConfigurations&lt;/key&gt; &lt;dict&gt; &lt;key&gt;UIWindowSceneSessionRoleApplication&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;UISceneConfigurationName&lt;/key&gt; &lt;string&gt;Default Configuration&lt;/string&gt; &lt;key&gt;UISceneDelegateClassName&lt;/key&gt; &lt;string&gt;Presentation.SceneDelegate&lt;/string&gt; &lt;/dict&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/dict&gt; // In the AppDelegate public func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -&gt; UISceneConfiguration { // Refer to a static UISceneconfiguration listed in the info.plist file return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) } As shown above, the Presentation.SceneDelegate is referred in the Info.plist file and the reference is removed from the AppDelegate (in EntryPoint library). The app target compiles, but when I run it, the SceneDelegate is not invoked. None of the methods from the SceneDelegate (scene(_:willConnectTo:options:), sceneDidDisconnect(_:), sceneDidEnterBackground(_:) etc.) are invoked. I only get the AppDelegate logs. It seems like the Configuration is ignored because it was incorrect. Any thoughts? Is it possible to move the SceneDelegate in this situation?
0
1
119
1d
Removing Grid Lines from Charts in SwiftUI
Hi, I have created a line graph using LineMark in Charts, which by default includes grid lines and axes lines. My requirement is to remove the grid lines but retain the axes lines and the values. I have tried the following code: .chartXAxis { AxisMarks(preset: .extended, values: .stride(by: 2), stroke: StrokeStyle(lineWidth: 0)) } This is removing grid lines as well as axes lines. How to retain axes lines while removing grid lines ?
1
0
127
1d
Swipe gestures for widgets
Hey, I am building some widgets and I'm quite surprised that Swipe gestures for widgets is not supported. It means the user must sacrifice home screen real estate to view multiple widgets to receive the same information. Ideally, swiping left / right inside of the widget should give a developer access to present different views. I realize that it means that a user would need to swipe outside of the widget, (or swipe to the beginning/end of the series of views inside of the widget) for the page to swipe, but I'd argue that this is the intuitive behavior of what widget scrollview would or should look like anyway.
1
0
152
2d
FocusState Issue with TextFields in ScrollView on iPad (iOS 18.0)
When using a VStack containing two TextFields inside a ScrollView on an iPad running iOS 18.0, the FocusState of the topmost TextField does not trigger, while the second TextField's FocusState works correctly. Adding an invisible TextField on top resolves the issue, but it appears to be a bug specifically in iOS 18.0 on iPads. This issue does not occur on iOS versions below or above 18.0 (including iOS 18.1). Code that is not working struct ContentView: View { @State var text: String = "" @FocusState private var focusState: Bool var body: some View { ScrollView(.vertical, content: { VStack(spacing: 0) { TextField(text: $text) { Text("Hello, World!") } .border(focusState ? Color.red : Color.gray) .focused($focusState) .onChange(of: focusState) { oldValue, newValue in print(newValue) } .onChange(of: text) { oldValue, newValue in focusState = true } } }) .padding() } } Code that is working struct ContentView: View { @State var text: String = "" @FocusState private var focusState: Bool var body: some View { ScrollView(.vertical, content: { VStack(spacing: 0) { // Invisible Text Field TextField("", text: .constant("")) .frame(height: 0) TextField(text: $text) { Text("Hello, World!") } .border(focusState ? Color.red : Color.gray) .focused($focusState) .onChange(of: focusState) { oldValue, newValue in print(newValue) } .onChange(of: text) { oldValue, newValue in focusState = true } } }) .padding() } }
0
0
160
2d
How to post openWindow, closeSpecificWindow, hideSpecificWindow from a swift class?
I am considering of shifting my codebase from appkit to SwiftUI entry point. In Appkit, we get control on each NSWindow. So that we can hide/resize window, close window and controll when to present a specific window . Because i have access to NSWindow instance which i can store and perform these actions. But when i shift to SwiftUI entry point, i declare struct conforming to SwiftUI Scene. And new windows will be created with the instance of this scene. I am using NSViewControllerRepresentable to add a NSViewController to the hierarchy of these scene. And adding content to this NSViewController's instance to show on screen. I need help in controlling the size of these windows. How can i close specific window ? Resize specific window ? or Hide specific window? If i use purely SwiftUI view's , then i can do this by using the Enviorment propery and use DismissWindow to close a window or openWindow with identifier to open a specific window by passing the specificer . But i am using Appkit's NSViewController where i will add buttons in heirarchy from which i want to trigger these events . And in that case how can i controll a specific window in case of multiwindow application?
2
0
132
1d
Closure with typed throws stored as a View property crashes on iOS 17
I've encountered an issue where storing a throws(PermissionError) closure as a property inside a SwiftUI View causes a runtime crash on iOS 17, while it works correctly on iOS 18. Here’s an example of the affected code: enum PermissionError: Error { case denied } struct PermissionCheckedView<AllowedContent: View, DeniedContent: View>: View { var protectedView: () throws(PermissionError) -> AllowedContent var deniedView: (PermissionError) -> DeniedContent init( @ViewBuilder protectedView: @escaping () throws(PermissionError) -> AllowedContent, @ViewBuilder deniedView: @escaping (PermissionError) -> DeniedContent ) { self.protectedView = protectedView self.deniedView = deniedView } public var body: some View { switch Result(catching: protectedView) { case .success(let content): content case .failure(let error): deniedView(error) } } } @main struct TestApp: App { var body: some Scene { WindowGroup { PermissionCheckedView { } deniedView: { _ in } } } } Specifically this is the stack trace (sorry for the picture I didn't know how to get the txt): If I use var protectedView: () throws -> AllowedContent without typed throws it works.
1
0
125
1d