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

Post

Replies

Boosts

Views

Activity

Unable to tap SwiftUI menu with XCUITest (ios15)
I have a SwiftUI menu Menu{ .... }, label : { Image(...).accessibility(identifier: "cardMenu") } I used to be able to bring up the menu (before upgrading to xcode 13 (ios15)) like this let app = XCUIApplication() app.launch() app.buttons["cardMenu"].tap() But now i am unable to see the identifier in app.buttons. Can't seem to find the identifier anymore. I've tried looking for the identifier in the other app fields and changing to use text instead of identifer. No luck. These tests used to work prior to the upgrade. Any help would be appreciated
3
1
3.4k
Sep ’21
Can NSTextView work with custom NSTextContentManager implementation? (TextKit2)
I'm trying to implement custom NSTextContentManager and use it with NSTextView, however it seems that NSTextView expect NSTextContentStorage all the time. final class MyTextContentManager: NSTextContentManager { // ... } It's added to layout manager, and NSTextView instance finds it properly: let textContentManager = MyTextContentManager() textContentManager.addTextLayoutManager(textLayoutManager) however, when I use it, I see errors at: [MyTextContentManager textStorage]: unrecognized selector sent to instance 0x600003d84870 the textStorage property is part of NSTextStorageObserving, that is not NSTextContentManager interface. It looks like NSTextView is not ready to work with custom NSTextContentManager. What did I miss?
3
0
2.6k
Sep ’21
Contents of .sheet() are being redrawn after dismiss and not presenting
Use Case If you have a ContentView that displays a pausable view based on a @State private var presentSheet = false property that is also used to present a .sheet(isPresented: $presentSheet) if: the property is sent to the PausableView(isPaused: presentSheet) as a normal property, the body of the ContentView and the body of the sheet is being redrawn when the sheet is dismissed the property is sent to the PausableView(isPaused: $presentSheet) as a @Binding, the body of the ContentView and the body of the sheet is NOT redrawn when the sheet is dismissed Is this normal behavior? The ContentView body makes sense to change, but is the sheet's body also supposed to be redrawn when the sheet is not presenting anymore after dismiss? Sample Project A sample project created in Xcode 13 is available here: https://github.com/clns/SwiftUI-sheet-redraw-on-dismiss. I noticed the same behavior on iOS 14 and iOS 15. There are also 2 animated gifs showing the 2 different behaviors in the GitHub project. The relevant code is in ContentView.swift: import SwiftUI struct DismissingView: View { @Binding var isPresented: Bool var body: some View { if #available(iOS 15.0, *) { print(Self._printChanges()) } else { print("DismissingView: body draw") } return VStack { Button("Dismiss") { isPresented.toggle() } Text("Dismissing Sheet").padding() }.background(Color.white) } } struct PausableView: View { var isPaused: Bool // @Binding var isPaused: Bool private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @State private var counter = 0 var body: some View { Text("Elapsed seconds: \(counter)") .onReceive(timer) { _ in counter += isPaused ? 0 : 1 } } } struct ContentView: View { @State private var presentSheet = false var body: some View { if #available(iOS 15.0, *) { print(Self._printChanges()) } else { print("ContentView: body draw") } return VStack{ Button("Show Sheet") { presentSheet.toggle() } Text("The ContentView's body along with the .sheet() is being redrawn immediately after dismiss, if the @State property `presentSheet` is used anywhere else in the view - e.g. passed to `PausableView(isPaused:presentSheet)`.\n\nBut if the property is passed as a @Binding to `PausableView(isPaused:$presentSheet)`, the ContentView's body is not redrawn.").padding() PausableView(isPaused: presentSheet) // PausableView(isPaused: $presentSheet) } .sheet(isPresented: $presentSheet) { DismissingView(isPresented: $presentSheet) .background(BackgroundClearView()) // to see what's happening under the sheet } } }
2
1
1.5k
Oct ’21
SwiftUI and focusedSceneValue on macOS, what am I doing wrong?
I'm trying to understand how to use .focusedSceneValue on macOS. Given a very basic app which displays Thing-s, and have a menu for editing the things. When I run the app, nothing is selected at first and the menu is disabled. When selecting e.g. the Thing Alfa in the sidebar. the menu becomes enabled as expected. When I select another Thing, the menu is also updated as expected. However, if I switch focus to another application, e.g. the Finder, and then switch back to my app, the menu is now disabled, even though a Thing is selected in the sidebar. If I open another window within my app and select e.g. Gamma in the sidebar of that window the menu is updated as expected. But, when switching back to the first window the menu is disabled, although a Thing is selected. What am I doing wrong? Xcode 13.1 and macOS Monterey 12.0.1. See the code below (the code can also be found here: https://github.com/danwaltin/FocusedSceneValueTest) struct Thing: Identifiable, Hashable { let id: Int let name: String static func things() -> [Thing] { return [ Thing(id: 1, name: "Alfa"), Thing(id: 2, name: "Beta"), Thing(id: 3, name: "Gamma") ] } } @main struct FocusedSceneValueTestApp: App { var body: some Scene { WindowGroup { ContentView() } .commands { ThingCommands() } } } struct ContentView: View { var body: some View { NavigationView { List(Thing.things()) { thing in NavigationLink( destination: DetailView(thing: thing), label: {Text(thing.name)} ) } Text("Nothing selected") } } } struct DetailView: View { let thing: Thing var body: some View { Text(thing.name) .focusedSceneValue(\.selectedThing, thing) .navigationTitle(thing.name) } } struct ThingCommands: Commands { @FocusedValue(\.selectedThing) private var thing var body: some Commands { CommandMenu("Things") { Button("Edit \(thingName)") { print("*** Editing \(thingName)") } .disabled(thing == nil) .keyboardShortcut("e") } } private var thingName: String { guard let thing = thing else { return "" } return thing.name } } struct SelectedThingKey : FocusedValueKey { typealias Value = Thing } extension FocusedValues { var selectedThing: Thing? { get {self[SelectedThingKey.self]} set {self[SelectedThingKey.self] = newValue} } }
2
0
1.8k
Oct ’21
Why would i not activate 'Preserve Vector Data' for SVGs?
First off, i believe i do know what the option 'Preserve Vector Data' does. But just in case, let me recap: When using PDFs or SVGs in my Asset Catalog, Xcode by default creates assets at fixed sizes from these files at buildtime. When i tick the 'Preserve Vector Data' box, the asset is scaled at runtime using the vector data, allowing for smooth scaling and crisp images at any scale. But the question i'm asking myself now is what exactly is the drawback - most likely performance-wise - to simply activating this option for each an every SVG or PDF Asset i use in my project? I would be very happy if someone could elaborate on this or direct me to some more in-depth documentation on Vector Assets :)
2
2
3.9k
Nov ’21
NSApplicationDelegate openURLs never called
Hi, I have an existing AppKit-based Mac app that I have been working on for a few years. For a new feature, I wanted to have the app opened by a different app, so I setup the URL scheme under CFBundleURLTypes in my Info.plist, and adopted this delegate callback:   - (void)application: (NSApplication *)application openURLs:(nonnull NSArray<NSURL *> *)urls Now when I invoke the URL from the 2nd app, it opens my app correctly, BUT this delegate method isn't called. What's interesting is that if I make a totally new app with a URL scheme and adopt this delegate method, it gets called without a problem! SO what about my original project could be responsible for this 'opensURLs' method to not be called? I've been searching for a solution for a couple of days without any luck. The macOS app's target has a Deployment Target of 10.15 and I'm running this on macOS12.0 with Xcode 13.
2
0
705
Nov ’21
WKExtensionsDelegateClassName is Invalid in info.plist
So I am banging my head, I realized my stand along Watch App had a STUPID long name of "App Name - WatchKit App" so I went into my Target and changed the Display Name to "App Name" removing WatchKit App. Well now my app won't validate when uploading to the Appstore. I get the message - Invalid Info.plist key. The key WKExtensionDelegateClassName in bundle App Name.app/Watch/App Name WatchKit App.app is invalid.  My Info.plist has the value of <key>WKExtensionDelegateClassName</key> <string>$(PRODUCT_MODULE_NAME).ExtensionDelegate</string> I have confirmed that I have  @WKExtensionDelegateAdaptor(ExtensionDelegate.self) var delegate in my @main for the SwiftUI App. And when I print a few values in my app launch I get the following confirmations: Super Init - ExtensionDelegate Contentview applicationDidFinishLaunching for watchOS Super Init - ExtensionDelegate Optional(My_App_Extension.Setup) Optional(My_App_Extension.Statistics) Optional(My_App_Extension.Other) applicationDidBecomeActive for watchOS update complication I create three classes at launch and print this in the log with print(ExtensionDelegate.shared.Setup as Any) , etc. The other lines are just confirming where I am at app startup. This is a WatchOS8 application and I am running Xcode version Version 13.1 (13A1030d).
2
1
1.3k
Nov ’21
Open a specific folder using UIDocumentPickerViewController
I'm using UIDocumentPickerViewController to import document to my app from OneDrive and I want to show the OneDrive folder every time I use UIDocumentPickerViewController instead of the last folder I opened. Is it possible? Can I use pickerController.directoryURL ? And how to get folder URL of OneDrive? class ViewController: UIViewController, DocumentDelegate {   var picker: DocumentPicker?       override func viewDidLoad() {     super.viewDidLoad()     picker = DocumentPicker(presentationController: self, delegate: self)   }   @IBAction func create_picker(_ sender: Any) {     picker?.presentDocumentPicker()   }       func didPickImage(image: UIImage?) {} } protocol DocumentDelegate: AnyObject {   func didPickImage(image: UIImage?) } class DocumentPicker: NSObject {   private var pickerController: UIDocumentPickerViewController?   private weak var presentationController: UIViewController?   private weak var delegate: DocumentDelegate?   init(presentationController: UIViewController,      delegate: DocumentDelegate) {     super.init()     self.presentationController = presentationController     self.delegate = delegate   }       func presentDocumentPicker() {     pickerController = UIDocumentPickerViewController(forOpeningContentTypes: [.image])     if let pickerController = pickerController {       pickerController.delegate = self       pickerController.allowsMultipleSelection = false       presentationController?.present(pickerController, animated: true)     }   } } extension DocumentPicker: UIDocumentPickerDelegate {   func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {     guard let url = urls.first else { return }     print(url)   } }
2
0
972
Nov ’21
How to toggle CoreData CloudKit sync during App runtime
Hi, I'm currently developing a SwiftUI based app with Core Data and CloudKit sync with the NSPersistentCloudKitContainer. I found different solutions how to toggle CloudKit sync of the Core Data during runtime. The basic idea of these solutions is the following. instantiate a new NSPersistentCloudKitContainer set storeDescription.cloudKitContainerOptions = nil load persistence store Some solutions recommend to restart the app manually to avoid exactly my problem. Issues So far so good. How can I distribute the new viewContext through my app during runtime. In the main App I distributed the viewContext during startup via @Environment(\.managedObjectContext) and it seems not be updated automatically after a reinitialization of NSPersistentCloudKitContainer. var body: some Scene {   WindowGroup {             ContentView()                 .environment(\.managedObjectContext, persistence.container.viewContext)         } } After deactivating the CloudKit sync I receive the following error when I try to add a new entity. [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'TestEntity' so +entity is unable to disambiguate. Any ideas? Regards Sven
5
1
2.5k
Nov ’21
fullScreenCover not dismissed if binding changes rapidly.
I'm working on an app targeting iOS 15+ using SwiftUI. The app has several Views that load data from an API in their onAppear() method. While the loading operation is in progress, these views show a loading overlay via .fullScreenCover(). While most of the time this works as expected, I've discovered that if the API operation completes before the overlay's .onAppear() has fired, the overlay gets stuck on screen, i.e. does not dismiss. This bug occurs both in the simulator and on device. This is a simplified version of my implementation: struct MyDataView: View { @EnvironmentObject var store:Store var Content: some View { // ... } @ViewBuilder var body: some View { let showLoadingOverlay = Binding( get: { store.state.loading }, set: { _ in } ) Content .onAppear { store.dispatch(LoadData) } .fullScreenCover(isPresented: showLoadingOverlay) { LoadingOverlay() } } } Log messages tell me that my store is updating correctly, i.e. the booleans all operate as expected. Adding log output to the binding's getter always prints the correct value. Adding a breakpoint to the binding's getter makes the problem disappear. I've found that the chronology of events that lead to this bug is: MyDataView.onAppear() LoadData Binding: true Overlay starts animating in LoadData finishes Binding: false Overlay fires it's onAppear I.e. whenever loading finishes before the fullScreenCover's onAppear is fired, the overlay get's stuck on screen. As long as loading takes at least as long as it takes the overlay to appear, the bug does not occur. It appears to be a race condition between the .fullScreenCover appearing and the binding changing to false. I've found that the bug can be avoided if loading is triggered in the overlay's .onAppear(). However, I would like to avoid this workaround because the overlay is not supposed to carry out data loading tasks.
2
1
1.2k
Dec ’21
Is it possible to unload/change the PersistenceStore in a SwiftUI App during runtime
Hi, I want to activate/deactivate the CloudKit Sync during App runtime in a user settings view. Basically this works fine. Every time I toggle between the NSPersistentContainer and the NSPersistentCloudKitContainer, I increase the persistence.persistenceContainerReloaded attribute and the whole view hierarchy will be reloaded. Thus all changes are passed through the whole app. During the reload phase I have to load a new persistence store by container.loadPersistentStores(...). Unfortunately, I cannot remove the old persistence store before loading the new one. The app crashes immediately, because the store and viewContext is still in use. Therefore, I just create a new one and trigger the reload. Afterwards every view is using the new viewContext. But somewhere in the background there is still the old persistence store with CloudKit Sync active and pushes every local change to the cloud. Changes on the cloud from other devices are not received anymore. Does someone has any idea, how to correctly unload a PersistentStore (replace NSPersistentCloudKitContainer by NSPersistentContainer) in a SwiftUI based app? @main struct TargetShooterApp: App {     @StateObject var persistence: Persistence = Persistence.shared     var body: some Scene {         WindowGroup {             ContentView()                 .environment(\.managedObjectContext, persistence.container.viewContext)                 .id(persistence.persistenceContainerReloaded)         }     } }
6
2
1.2k
Dec ’21
Use @AppStorage with Arrays
Hey, I know you can write @AppStorage("username") var username: String = "Anonymous" to access a Value, stored In the User Defaults, and you can also overwrite him by changing the value of username. I was wondering if there is any workaround to use @AppStorage with Arrays. Because I don't find anything, but I have a lot of situations where I would use it. Thanks! Max
1
1
1.1k
Dec ’21
How to get upload progress when using "await urlSession.upload()"
How to get upload progress when using: let (data, urlResponse) = try await urlSession.upload( for: urlRequest, from: bodyData, delegate: nil // Something I need here maybe? ) I have tried using: func urlSession( _ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { print("fractionCompleted : \(Float(totalBytesSent) / Float(totalBytesExpectedToSend))") } Within the same class but it never fires. I specifically want the async / await capability on the upload. I couldn't get the session.uploadTask to work with the await prefix.
2
1
2.3k
Jan ’22
-[NSView (un)lockFocus] deprecated, what to use instead?
I am hitting major road blocks in migrating one of my Obj-C-Cocoa applications away from -[NSView (un)lockFocus] and -[NSBitmapImageRep initWithFocusedViewRect:]. In a transcript of a presentation on WWDC2018 I read: With our changes to layer backing, there's a few patterns I want to call out that aren't going to work in macOS 10.14 anymore. If you're using NSView lockFocus and unlockFocus, or trying to access the window's graphics contents directly, there's a better way of doing that. You should just subclass NSView and implement draw rect. ... Of course, we all implemented -[NSView drawRect:] for decades now. The big question is, how can we do incremental (additional, event driven) drawing in our views, without redrawing the whole view hierarchy. This is the use case of -(un)lockFocus, and especially when drawing of the base view is computational expensive. Wo would have thought that people use -(un)lockFocus for regular drawing of the NSView hierarchy. I tried to get away with CALayer, only to find out after two days experimenting with it, that a sublayer can only be drawn if the (expensive) main layer has been drawn before —> dead end road. Now I am going to implement a context dependent -[NSView drawRect:]. Based on a respective instance variable, either of the (expensive) base presentation of the view or the simple additions are drawn. Is it that what Apple meant by … just subclass NSView and implement draw rect? From the point of view of object oriented programming, using switch() in methods to change the behaviour of the object is ugly - to say the least. Any better options? Ugly or not, in any case, I don’t want to redraw the whole view hierarchy only for moving a crosshairs in a diagram. My actual use case is: This application draws into a custom diagram NSView electrochemical measurement curves which may consist of a few thousands up to millions of data points. The diagram view provides a facility for moving crosshairs and other pointing aids over the displayed curves, by dragging/rolling with the mouse or the touch pad, or by moving it point by point with the cursor keys. Diagram generation is computational expensive and it must not occur only because the crosshairs should be moved to the next data point. So for navigating the crosshairs (and other pointing aids), a respective method locks the focus of said view, restores the background from a cache, caches the background below the new position of the crosshairs using -[NSBitmapImageRep initWithFocusedViewRect:], draws the crosshairs and finally unlocks the focus. All this does not work anymore since 10.14.
3
0
1.4k
Jan ’22
@FetchRequest predicate is ignored if the context changes
I have a simple SwiftUI application with CoreData and two views. One view displays all "Place" objects. You can create new places and you can show the details for the place. Inside the second view you can add "PlaceItem"s to a place. The problem is that, once a new "PlaceItem" is added to the viewContext, the @NSFetchRequest seems to forget about its additional predicates, which I set in onAppear. Then every place item is shown inside the details view. Once I update the predicate manually (the refresh button), only the items from the selected place are visible again. Any idea how this can be fixed? Here's the code for my two views: struct PlaceView: View { @FetchRequest(sortDescriptors: []) private var places: FetchedResults<Place> @Environment(\.managedObjectContext) private var viewContext var body: some View { NavigationView { List(places) { place in NavigationLink { PlaceItemsView(place: place) } label: { Text(place.name ?? "") } } } .toolbar { ToolbarItem(placement: .primaryAction) { Button { let place = Place(context: viewContext) place.name = NSUUID().uuidString try! viewContext.save() } label: { Label("Add", systemImage: "plus") } } } .navigationTitle("Places") } } struct PlaceItemsView: View { @ObservedObject var place: Place @FetchRequest(sortDescriptors: []) private var items: FetchedResults<PlaceItem> @Environment(\.managedObjectContext) private var viewContext func updatePredicate() { items.nsPredicate = NSPredicate(format: "place == %@", place) } var body: some View { NavigationView { List(items) { item in Text(item.name ?? ""); } } .onAppear(perform: updatePredicate) .toolbar { ToolbarItem(placement: .primaryAction) { Button { let item = PlaceItem(context: viewContext) item.place = place item.name = NSUUID().uuidString try! viewContext.save() } label: { Label("Add", systemImage: "plus") } } ToolbarItem(placement: .navigationBarLeading) { Button(action: updatePredicate) { Label("Refresh", systemImage: "arrow.clockwise") } } } .navigationTitle(place.name ?? "") } } struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext var body: some View { NavigationView { PlaceView() } } } Thanks!
2
0
816
Feb ’22