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
Posts under UI Frameworks topic

Post

Replies

Boosts

Views

Activity

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?
2
0
332
Mar ’25
Lazy cell registration causes crash while dequeue the collectionview cell in collectionview
Cell Registration lazy var headerCell: UICollectionView.SupplementaryRegistration<UICollectionReusableView> = .init(elementKind: UICollectionView.elementKindSectionHeader) { supplementaryView, elementKind, indexPath in } and Cell Dequeue datasource.supplementaryViewProvider = { [weak self] collectionView, kind, indexPath in return collectionView.dequeueConfiguredReusableSupplementary(using: headerCell, for: indexPath) } I am registering a collectionview cell or collwctionview reusable view as lazy variable. It causes a crash like Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue a supplementary view using a registration that was created inside -collectionView:viewForSupplementaryElementOfKind:atIndexPath: or inside a UICollectionViewDiffableDataSource supplementary view provider. Creating a new registration each time a supplementary view is requested will prevent reuse and cause created supplementary views to remain inaccessible in memory for the lifetime of the collection view. Registrations should be created up front and reused. Registration: <UICollectionViewSupplementaryRegistration: 0x600001798a00>
2
0
367
Feb ’25
Reliable APIs to check if a Hotkey/Shortcut is already in use?
In our application we have two usecases for a Hotkey/Shortcut identification API/method. We have some predefined shortcuts that will ship with our MacOS application. They may or may not change dynamically, based on what the user has already set as shortcuts/hotkeys, and also to avoid any important system wide shortcuts that the user may or may not have changed. We allow the user to customize the shortcuts/hotkeys in our application, so we want to show what shortcuts the user already has in use system-wide and across their OS experience. This gives rise to the need for an API that lets us know which shortcut/hotkeys are currently being used by the user and also the current system wide OS shortcuts in use. Please let me know if there are any APIs in AppKit or SwiftUI we can use for the above
0
0
249
Mar ’25
scenePhase not behaving as expected on screen lock
Seeing weird sequences of changes when locking the screen when view is visable. .onChange(of: scenePhase) { phase in if phase == .active { if UIApplication.shared.applicationState == .active { print("KDEBUG: App genuinely became active") } else { print("KDEBUG: False active signal detected") } } else if phase == .inactive { print("KDEBUG: App became inactive") // Handle inactive state if needed } else if phase == .background { print("KDEBUG: App went to background") // Handle background state if needed } } seen: (locks screen) KDEBUG: App became inactive KDEBUG: App genuinely became active KDEBUG: App went to background expected (locks screen) KDEBUG: App became inactive KDEBUG: App went to background
2
0
111
Apr ’25
SwiftUI subview .frame ignored on parent view appear, MacOS
When a parent view is selected for the detail pane of a NavigationSplitView subviews appear as expected but not with the dimensions set by .frame on the subview. Toggling the flag works as expected, appearing the subview with the idealWidth. I persist the flag in a SwiftData @Model class so that on restart and first appearance of the parent view the Right View subview presence is as it was left. The problem is that the .frame size is ignored, apparently. No manner of programatic view refresh seems to trigger a resize to the preferred values, only toggling the flag. Is there a better way to handle a collapsing subview in an HSplitView? Why is the .frame not respected? In this example I've added the else clause so HSplitView always has two views with .frame settings but the result is the same without it. VStack { HSplitView { VStack { Text("left view") } .frame( minWidth: 100, idealWidth: .infinity, maxWidth: .infinity, maxHeight: .infinity ) if documentSettings.nwIsPieChartShowing { VStack { Text("right view") } .frame( minWidth: 100, idealWidth: 200, maxWidth: .infinity, maxHeight: .infinity ) } else { Text("") .frame( minWidth: 0, idealWidth: 0, maxWidth: 0, maxHeight: .infinity ) } } HStack { Button("Right View", systemImage: { documentSettings.nwIsPieChartShowing ? "chart.pie.fill" : "chart.pie"}(), action: { documentSettings.nwIsPieChartShowing.toggle() } ) } } } } MacOS Sequoia 15.3.1, Xcode 16.2
Topic: UI Frameworks SubTopic: SwiftUI Tags:
2
0
270
Mar ’25
Multipeer Connectivity with iOS devices
I used the Multipeer Connectivity Framework to allow players of my game app to see the highest score along with their own score as the game progresses. It works fine running in 3 or 4 simulators in Xcode. However, I was just told by two Apple support reps that bluetooth connectivity is not allowed between two Apple devices, other than for watches, AirPods, headphones, etc. My question is: can two iPhones or iPads connect for peer-to-peer play? Can they play offline? If the answer is yes to either or both of those questions, how do I get that to happen since I get an error message when trying to connect two iPhones via bluetooth even though they can see each other in the Other Devices list? If the answer is no, then why does Apple provide a Multipeer Connectivity Framework and simulate that type of device to device connection with the Xcode simulators?
Topic: UI Frameworks SubTopic: SwiftUI
0
0
176
Feb ’25
Removing SwiftUI View from hierarchy
In a UIKit application, removing a view from the hierarchy is straightforward—we simply call myView.removeFromSuperview(). This not only removes myView from the UI but also deallocates any associated memory. Now that I'm transitioning to SwiftUI, I'm struggling to understand the recommended way to remove a view from the hierarchy, given SwiftUI's declarative nature. I understand that in SwiftUI, we declare everything that should be displayed. However, once a view is rendered, what is the correct way to remove it? Should all UI elements be conditionally controlled to determine whether they appear or not? Below is an example of how I’m currently handling this, but it doesn’t feel like the right approach for dynamically removing a view at runtime. Can someone guide me on the best way to remove views in SwiftUI? struct ContentView: View { @State private var isVisible = true var body: some View { VStack { if isVisible { // set this to false to remove TextView? Text("Hello, SwiftUI!") .padding() } Button("Toggle View") { ... } } } }
1
0
272
Mar ’25
What happened to readable margins?
Am in the process of migrating some UIKit based apps over to SwiftUI, but for the life of me I cannot find the SwiftUI equivalent of Readable Content Margins. I have come across some workarounds that kind of, sort of work, but do not produce the same results when compared to running the same user interface written using UIKit on several sizes of iPads in portrait and landscape orientiations. is it something Apple has not gotten around to yet, because I realize SwiftUI is a work-in-progress, or do we not care about creating consistent readable margins in our apps anymore?
2
0
325
Mar ’25
Trouble using MKAnnotation on a MKMapView
Hi everyone, i'im having troubles using Annotation on a Map View. I have a a core data model called Location that conforms to NSManagedObject from CoreData and MKAnnotation form MapKit, and i'm trying to add an array of Location to a MKMview instance in a UIViewController class, by doing somethhing like this: mapView.addAnnotation(locations), but xcode compliants with a strange error which says Argument type '[Location]' does not conform to expected type 'MKAnnotation'. it's strange to me because my Location class conforms to MKAnnotation protocol and i implemented the protocol's methods (coordinate, title, subtitle). Please can anyone help me how to fix this issues. Thank you all
Topic: UI Frameworks SubTopic: UIKit Tags:
2
0
177
Mar ’25
swiftui fileimporter inside UIHostingController
I'm working on an old iOS app that started with objective-C + UIKit and has being migrated to Swift + SwiftUI. Currently its code is mostly Swift + SwiftUI but it has still some objective-C and some UIKit ViewControllers. One of the SwiftUI views uses fileImporter to open Files App and select a file from the device. This has been working well until iOS 18 is launched. With iOS 18 the file picker is not launching correctly and is frozen in every simulator (the unique real device I've could test with iOS 18 seemed to work correctly). I managed to clone my project and leave it with the minimal amount of files to reproduce this error. This is the code: AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> {} @property (strong, nonatomic) UIWindow *window; @end AppDelegate.m #import "AppDelegate.h" #import "MyApp-Swift.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; FirstViewBuilder *viewBuilder = [[FirstViewBuilder alloc] init]; [viewBuilder show]; return YES; } @end FirstViewBuilder.swift import SwiftUI @objc class FirstViewBuilder: NSObject { private var view: UIHostingController<FirstView> @objc override init() { self.view = MyHostingController(rootView: FirstView()) } @objc func show() { let app = UIApplication.shared.delegate as? AppDelegate let window = app?.window window?.backgroundColor = .white // Use navigationController or view directly depending on use window?.rootViewController = view } } FirstView.swift import SwiftUI struct FirstView: View { @State var hasToOpenFilesApp = false var body: some View { VStack(alignment: .leading, spacing: 0) { Button("Open Files app") { hasToOpenFilesApp = true }.fileImporter(isPresented: $hasToOpenFilesApp, allowedContentTypes: [.text]) { result in switch result { case .success(let url): print(url.debugDescription) case .failure(let error): print(error.localizedDescription) } } } } } And finally, MyHostingController import SwiftUI class MyHostingController<Content>: UIHostingController<Content> where Content: View { override init(rootView: Content) { super.init(rootView: rootView) } @objc required dynamic init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() navigationItem.hidesBackButton = true } } Launching this in an iPhone 13 Pro (18.2) simulator I click on Open Files App, it takes 2 seconds to open it, and it opens full screen (not like a modal). Buttons on the top are behind the status bar and buttons at the bottom are behind the Home indicator. But it's worse because the user can't interact with this view, it's frozen. I created a fresh SwiftUI project just with this unique view and the fileimport worked as expected so I thought the problem was due to embed the SwiftUI view inside the UIHostingController. So I made these modifications to the minimal project: Remove the files AppDelegate, FirstViewBuilder and MyHostingController. Create this SwiftUI App file import SwiftUI @main struct MyApp: App { var body: some Scene { WindowGroup { FirstView() } } } And again the same problem with iOS 18. But if I launch this exact project in an iPhone 13 Pro (17.4) simulator and open the files apps (now it opens almost instantly) it works OK and shows the file picker as a modal, as expected, and I can interact with it and select files. Last thing I've tried is removing LaunchScreen.xib from my project and Launch screen interface file base name key from my info.plist but the problem keeps happening. I guess it must be due to my project configuration (too old) but I have no more ideas of where to look at. The possibility of having a fresh SwiftUI project and "move" the old project to the new one could take me several weeks and I discard it by the moment. Could I use another method to select files from SwiftUI views with iOS 18?
2
0
502
Feb ’25
Document-based app failing in Mac Catalyst
A document-based app that's been running fine on iPad and Mac Catalyst has stopped working on the Mac as of Sequoia 15.3. After the user selects a document to open, my app never gets called back from Apple's framework. (The app works fine on iPadOS.) Anybody else see this? I filed a feedback, FB16506048, several weeks ago but have had no reply.
Topic: UI Frameworks SubTopic: SwiftUI
1
0
190
Mar ’25
On iOS 16, VoiceOver will not speak "¥1,230" in Japanese.
In iOS 16, VoiceOver no longer speaks numbers such as “¥1,230" in Japanese, which worked correctly in iOS 15 and earlier. This is a very important bug in Accessibility support. Steps to reproduce Create iOS App Project on Xcode. (I used Xcode 14) Set the Development Localization to "Japanese" on the project settings. (to make VoiceOver speak Japanese) For example, write the following code. Build and run the App targeting the actual device, not the simulator. Actual devices running iOS 15 or earlier will correctly read out loud with VoiceOver, while iOS 16 will have some reading problems. import SwiftUI @main struct VoiceOverProblemApp: App {   var body: some Scene {     WindowGroup {       // on project settings, set the development localization to Japanese.       VStack {         // ✅ said "Hello, world!"         Text("Hello, world!")                   // ✅ said "残高 (ざんだか, zan-daka)"         Text("残高")                   // ❌ said nothing (until iOS 15 or earlier, said "千二百三十円 (せんにひゃくさんじゅうえん, sen-nihyaku-san-ju-yen)"         Text("¥1,230")                   // ❌ said wrong         //  correct (iOS 15 or earlier):  "残高は千二百三十円です (zan-daka wa sen-nihyaku-san-ju-yen desu)"         //  incorrect (iOS 16)      : "残高はです (zan-daka wa desu)"         Text("残高は ¥1,230 です")       }     }   } } The above sample code uses SwiftUI's Text but the same problem occurs with UIKit's UILabel.
1
0
1.2k
Apr ’25
SwiftUI views lock up after background and sleep for “Designed for iPad” apps
There's an easily reproducible SwiftUI bug on macOS where an app's UI state no longer updates/re-renders for "Designed for iPad" apps (i.e. ProcessInfo.processInfo.isiOSAppOnMac == true). The bug occurs in Xcode and also if the app is running independent of Xcode. The bug occurs when: the user Hides the app (i.e. it goes into the background) the user puts the Mac to sleep (e.g. Apple menu > Sleep) a total of ~60 seconds transpires (i.e. macOS puts the app into the "suspended state") when the app is brought back into the foreground the UI no longer updates properly The only way I have found to fix this is to manually open a new actual full app window via File > New, in which case the app works fine again in the new window. The following extremely simple code in a default Xcode project illustrates the issue: import SwiftUI @main struct staleApp: App { @State private var isBright = true var body: some Scene { WindowGroup() { ZStack { (isBright ? Color.white : Color.black).ignoresSafeArea() Button("TOGGLE") { isBright.toggle(); print("TAPPED") } } .onAppear { print("\(isBright ? "light" : "dark") view appeared") } } } } For the code above, after Hiding the app and putting the computer to sleep for 60 seconds or more, the button no longer swaps views, although the print statements still appear in the console upon tapping the button. Also, while in this buggy state, i can get the view to update to the current state (i.e. the view triggered by the last tap) by manually dragging the corner of the app window to resize the window. But after resizing, the view again does not update upon button tapping until I resize the window again. so it appears the diff engine is mucked or that the Scene or WindowGroup are no longer correctly running on the main thread I have tried rebuilding the entire view hierarchy by updating .id() on views but this approach does NOT work. I have tried many other options/hacks but have not been able to reset the 'view engine' other than opening a new window manually or by using: @Environment(.openWindow) private var openWindow openWindow could be a viable solution except there's no way to programmatically close the old window for isiOSAppOnMac (@Environment(.dismissWindow) private var dismissWindow doesn't work for iOS)
0
0
159
Apr ’25
ShareLink of a movie is inconsistent
In my app, I have a ShareLink that attempts to share a movie. struct MovieTransferable: Transferable { let url: URL let writeMovie: (URL) -&gt; () static var transferRepresentation: some TransferRepresentation { DataRepresentation( exportedContentType: .movie, exporting: { (movie) in // Write the movie into documents. movie.writeMovie(movie.url) return try! Data(contentsOf: movie.url) }) FileRepresentation( exportedContentType: .movie, exporting: { (movie) in // Write the movie into documents. movie.writeMovie(movie.url) return SentTransferredFile(movie.url) }) } } The ShareLink works if you try to share the movie with the Photos app, Air Drop, and iMessage. If I share to WhatsApp, the movie shows up as empty (zero length), but there's a movie. If I share to Discord, the movie is not displayed at all (only the comment). Instagram posts a dialog saying it won't allow movies and to use the app (why are they even in the ShareLink option for movies?). YouTube processes for a bit and then does nothing (no upload). Are there things that I can do to make the Transferable accepted at more of the end points? It's at fps 30 and I've tried most of the available codec's. The size is the same as the iPhone's screen, so the aspect ratio is a bit odd. However, if I go directly to the app (Discord, etc...) upload from my phone works fine. Any help would be appreciated to make this more viable.
2
0
384
Mar ’25
UIContextMenuInteraction not working if view is originally offscreen
I’m having a weird UIKit problem. I have a bunch of views in a UIScrollView and I add a UIContextMenuInteraction to all of them when the view is first loaded. Because they're in a scroll view, only some of the views are initially visible. The interaction works great for any of the views that are initially on-screen, but if I scroll to reveal new subviews, the context menu interaction has no effect for those. I used Xcode's View Debugger to confirm that my interaction is still saved in the view's interactions property, even for views that were initially off-screen and were then scrolled in. What could be happening here?
Topic: UI Frameworks SubTopic: UIKit Tags:
0
0
106
Mar ’25
SwiftUI refresh while alert is visible
Does causing a swiftui view refresh via modifying observed properties while an alert is visible allowed? I am seeing a warning 'Presenting view controller <SwiftUI.PlatformAlertController> from detached view controller is not supported, and may result in incorrect safe area insets and a corrupt root presentation. Make sure <SwiftUI.PlatformAlertController> is in the view hierarchy before presenting from it. Will become a hard exception in a future release.' and the warning 'Attempt to present <SwiftUI.PlatformAlertController> on (from ) while a presentation is in progress'. The second warning occurs more often.
5
0
316
Mar ’25
Strange behavior when pushing UIViewController
This is a very strange behavior when pushing vc that I have never seen since I started coding. The pushed ViewController is transparent and only navBarTitle is shown. After the push, you can't control anything unless you go back to the home screen. STEPS TO REPRODUCE Long press currency change button below.(currencyWrapper) Call selectCountry and this bug happens. SourceCode let currencyWrapper = UIView() private func configureCurrencyCard(){ //The strange behavior shows up after long pressing this currencyWrapper.backgroundColor = .white currencyWrapper.addTarget(self, action: #selector(changeCurrency)) currencyWrapper.setWidth(currencyChangeIcon.follow(by: 16, x: true)) currencyWrapper.setCenterX(w1/2) currencyWrapper.setHeight(currencyLabel.follow(by: 12, x: false)) currencyWrapper.roundToCircle(true) view.addSubview(currencyWrapper) } private func selectCountry(country: Country){ let vc = CountryViewController(country: country) vc.hidesBottomBarWhenPushed = true navigationController?.pushViewController(vc, animated: true) }
Topic: UI Frameworks SubTopic: UIKit Tags:
2
0
244
Mar ’25
business
import SwiftUI struct Product: Identifiable { let id = UUID() let name: String let pricePerKg: Double } struct ContentView: View { @State private var selectedProduct: Product? @State private var quantity: Double = 1.0 @State private var orderDate = Date() @State private var showingConfirmation = false let products = [ Product(name: "Lamb", pricePerKg: 15.0), Product(name: "Beef", pricePerKg: 20.0), Product(name: "Chicken", pricePerKg: 10.0) ] var body: some View { NavigationView { Form { Section(header: Text("Select Meat")) { Picker("Meat Type", selection: $selectedProduct) { ForEach(products) { product in Text(product.name).tag(product as Product?) } } } if let selectedProduct = selectedProduct { Section(header: Text("Quantity (kg)")) { Stepper(value: $quantity, in: 0.5...10, step: 0.5) { Text("\(quantity, specifier: "%.1f") kg") } } Section(header: Text("Delivery Date")) { DatePicker("Select Date", selection: $orderDate, in: Date()..., displayedComponents: .date) } Section(header: Text("Total Price")) { Text("$\(selectedProduct.pricePerKg * quantity, specifier: "%.2f")") } Button("Confirm Order") { showingConfirmation = true } .alert(isPresented: $showingConfirmation) { Alert(title: Text("Order Confirmed"), message: Text("You have ordered \(quantity, specifier: "%.1f") kg of \(selectedProduct.name) for \(orderDate.formatted(date: .long, time: .omitted))."), dismissButton: .default(Text("OK"))) } } } .navigationTitle("Halal Butcher") } } } @main struct HalalButcherApp: App { var body: some Scene { WindowGroup { ContentView() } } }
2
0
218
Mar ’25
Data fetching issue from SensorKit
I want SensorKit data for research purposes in my current application. I have applied for and received permission from Apple to access SensorKit data. During implementation, I encountered an issue in which no data was being retrieved despite granting all the necessary permissions. I am using did CompleteFetch & didFetchResult delegate methods for retrieving data from Sensorkit. CompleteFetch method calls but where I can find different event data like Device usage, Ambient Light, etc? & didFetchResult method does not call. Methods I am using: 1. func sensorReader(_ reader: SRSensorReader, didCompleteFetch fetchRequest: SRFetchRequest) 2. func sensorReader(_ reader: SRSensorReader, fetching fetchRequest: SRFetchRequest, didFetchResult result: SRFetchResult<AnyObject>) -> Bool Could anyone please assist me in resolving this issue? Any guidance or troubleshooting steps would be greatly appreciated.
0
0
379
Feb ’25