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

Suddenly getting "Trailing closure passed to parameter of type 'Visibility' that does not accept a closure" for toolbars
Not sure exactly what’s going on here. I’ve been building out this little app for the past couple of weeks using Xcode 16 betas, and in a couple of places I have code like this: EditItemView(item: inItem) .toolbar { ToolbarItem { Button(action: { self.printLabel(item: inItem) }) { Label("Print Label", systemImage: "printer.filled.and.paper") } } } Now all of a sudden I'm getting an error on .toolbar: “Trailing closure passed to parameter of type 'Visibility' that does not accept a closure.” Looking at the comment for toolbar, it says this method "specifies the visibility," which is a weird thing for it to do with a name like that. But it also has a deprecation tag that I don't quite understand: @available(macOS, introduced: 13.0, deprecated: 100000.0, renamed: "toolbarVisibility(_:for:)"). The name change makes sense, given the description. But what is the huge deprecation version number? Just a hack? And why is this code no longer compiling? As I'm commenting out instances of this, all my toolbars are failing in this way. ETA: yeah, I finally found the right declaration of .toolbar: func toolbar<Content>(@ViewBuilder content: () -> Content) -> some View where Content : View. It's no longer choosing that one. I hate Swift sometimes. ETA2: I finally whittled it down to it not liking Label for some reason. Replacing that with Image works. But of course, the compiler won't tell me why. ETA3: It's worse than I thought: I tried to make a small test case for a bug, and Label works just fine. Why does my code not?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
0
1
700
Oct ’24
Calling `requestAuthorization(options:completionHandler:)` in Swift 6 leads to `EXC_BAD_INSTRUCTION` crash.
I am in the process of evaluating Swift 6 and I noticed that when using the completion handler version of the requestAuthorization the application crashes with EXC_BAD_INSTRUCTION exception. Using this code: let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound, .badge]) { success, error in print(success) } Crashes when the project is build with Swift 6 and works normalising when build with Swift 5. The only solution that I have found so far is to switch to using the async version of that API: Task { let center = UNUserNotificationCenter.current() do { if try await center.requestAuthorization(options: [.alert, .sound, .badge]) == true { print("success") } else { print("fail") } } catch { print("Error") } } Is the a known issue? I have submitted feedback with ID "FB15294185".
3
1
840
Oct ’24
NavigationSplitView's toolbar item disappears on resize
When a NavigationSplitView's sidebar has a default selected value, the detail's toolbar item disappears when resizing the view from compact to regular size. @State private var selectedId: String? = "anything" NavigationSplitView { List(selection: $selectedId) { Text("Sidebar") } } detail: { Text("Detail") .toolbar { Button("Button") {} } } There is no way to get the toolbar item back, other than reloading the view. I have no idea why this happens. In my example I have a dummy Text("Sidebar") without a value, but the problem occurs regardless of its content. Tested on Simulator iPadOS 18.0, and I've seen this happen in our production app.
3
0
711
Oct ’24
Xcode 16 - List Lazy loading broken
In Xcode 16 and Xcode 16.1 beta 2 the lazy loading of list does not work properly. Tested on physical device with iOS 17. Below is minimal code to reproduce this issue: You can see in the debug console how many child views are initialized based on the print statement. import SwiftUI @main struct TestDemoApp: App { let data = Array(1...1000) var body: some Scene { WindowGroup { List(data, id: \.self) { item in SomeView(item: item) } } } } struct SomeView: View { static var count = 0 let item: Int init(item: Int){ self.item = item Self.count += 1 print(Self.count) } var body: some View{ Text(String(item)) .frame(height: 100) } } When the view is shown the List creates all child views at once as shown in the console output: It does not loads only first 13 out of 1000 as it does in older xcode 15.2: As the List is quite often used component in Swiftui Apps, the apps will be slow, because all the data are loaded and the main thread will be stuck until all child views are loaded.
6
4
1.3k
Oct ’24
Cannot convert value of type '[Lesson]' to expected argument type 'Binding<C>' and Generic parameter 'C' could not be inferred
I have a problem with ForEach loop, and when i debug the program there appear a problems: Cannot convert value of type '[Lesson]' to expected argument type 'Binding' Generic parameter 'C' could not be inferred Please help me import SwiftUI import Foundation struct ContentView: View { var lessons: [Lesson] = [] let todaysDate = Date() let lessonTime = Date()...Date().addingTimeInterval(45*60) let startProgress = Date().addingTimeInterval(-60) let endProgress = Date().addingTimeInterval(60) @State private var currentLesson: String = "Przerwa" var body: some View { let liveHourString = todaysDate.formatted(date: .omitted, time: .shortened) let liveHourString = "9:10" let liveWeekdayString = todaysDate.formatted(Date.FormatStyle().weekday(.wide)) ForEach(lessons) { lesson in if lesson.startTime <= liveHourString && lesson.endTime >= liveHourString && lesson.dayOfWeek == liveWeekdayString { currentLesson = lesson.name currentLessonStart = lesson.startTime } } VStack { ProgressView(timerInterval: startProgress...endProgress, countsDown: false) { Text(currentLesson) } currentValueLabel: { Text(currentLessonStart...endProgress) } .padding([.top, .leading, .trailing], 15.0) NavigationView{ List(lessons) { lesson in if lesson.dayOfWeek == todaysDate.formatted(Date.FormatStyle().weekday(.wide)) { VStack(alignment: .leading){ Text(lesson.name) Text("\(lesson.startTime) - \(lesson.endTime)") .foregroundStyle(.secondary) Text(lesson.room) .foregroundStyle(.secondary) } } } .navigationBarTitle(Text("Today's Lessons")) } } } } #Preview { ContentView(lessons: testData) } And i have also this file: struct Lesson: Identifiable { var id = UUID() var name: String var room: String var startTime: String var endTime: String var dayOfWeek: String } #if DEBUG let testData = [ //MONDAY Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "8:00", endTime: "8:45", dayOfWeek: "Monday"), Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "8:50", endTime: "9:35", dayOfWeek: "Monday"), Lesson(name: "Programowanie Aplikacji Webowych", room: "J3", startTime: "9:40", endTime: "10:25", dayOfWeek: "Monday"), Lesson(name: "Język Angielski", room: "Y2", startTime: "10:45", endTime: "11:30", dayOfWeek: "Monday"), Lesson(name: "Język Niemiecki", room: "Y4", startTime: "11:40", endTime: "12:25", dayOfWeek: "Monday"), Lesson(name: "Język Angielski", room: "Y2", startTime: "12:35", endTime: "13:20", dayOfWeek: "Monday"), Lesson(name: "Matematyka", room: "B10", startTime: "13:25", endTime: "14:10", dayOfWeek: "Monday"), //TUESDAY Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "8:50", endTime: "9:35", dayOfWeek: "Tuesday"), Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "9:40", endTime: "10:25", dayOfWeek: "Tuesday"), Lesson(name: "Programowanie Aplikacji Desktopowych", room: "X7", startTime: "10:45", endTime: "11:30", dayOfWeek: "Tuesday"), Lesson(name: "Programowanie Aplikacji Desktopowych", room: "X7", startTime: "11:40", endTime: "12:25", dayOfWeek: "Tuesday"), Lesson(name: "Testowanie i Dokumentowanie Aplikacji", room: "J5", startTime: "12:35", endTime: "13:20", dayOfWeek: "Tuesday"), Lesson(name: "Testowanie i Dokumentowanie Aplikacji", room: "J5", startTime: "13:25", endTime: "14:10", dayOfWeek: "Tuesday"), Lesson(name: "Projektowanie Oprogramowania", room: "J5", startTime: "14:15", endTime: "15:00", dayOfWeek: "Tuesday"), Lesson(name: "Projektowanie Oprogramowania", room: "J5", startTime: "15:05", endTime: "15:50", dayOfWeek: "Tuesday"), //WEDNESDAY Lesson(name: "Programowanie Aplikacji Mobilnych", room: "X1", startTime: "8:00", endTime: "8:45", dayOfWeek: "Wednesday"), Lesson(name: "Programowanie Aplikacji Mobilnych", room: "X1", startTime: "8:50", endTime: "9:35", dayOfWeek: "Wednesday"), Lesson(name: "Matematyka", room: "B10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Wednesday"), Lesson(name: "Matematyka", room: "B10", startTime: "10:45", endTime: "11:30", dayOfWeek: "Wednesday"), Lesson(name: "Język Polski", room: "A7", startTime: "11:40", endTime: "12:25", dayOfWeek: "Wednesday"), Lesson(name: "Język Polski", room: "A7", startTime: "12:35", endTime: "13:20", dayOfWeek: "Wednesday"), Lesson(name: "Historia", room: "L5", startTime: "13:25", endTime: "14:10", dayOfWeek: "Wednesday"), //THURSDAY Lesson(name: "Chemia", room: "C7", startTime: "8:00", endTime: "8:45", dayOfWeek: "Thursday"), Lesson(name: "WoS", room: "C8", startTime: "8:50", endTime: "9:35", dayOfWeek: "Thursday"), Lesson(name: "Matematyka", room: "B10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Thursday"), Lesson(name: "Programowanie Obiektowe", room: "X1", startTime: "10:45", endTime: "11:30", dayOfWeek: "Thursday"), Lesson(name: "Programowanie Obiektowe", room: "X1", startTime: "11:40", endTime: "12:25", dayOfWeek: "Thursday"), Lesson(name: "Język Polski", room: "A7", startTime: "12:35", endTime: "13:20", dayOfWeek: "Thursday"), //FRIDAY Lesson(name: "Język Angielski", room: "Y2", startTime: "8:00", endTime: "8:45", dayOfWeek: "Friday"), Lesson(name: "Chemia", room: "C7", startTime: "8:50", endTime: "9:35", dayOfWeek: "Friday"), Lesson(name: "Geografia", room: "C10", startTime: "9:40", endTime: "10:25", dayOfWeek: "Friday"), Lesson(name: "Matematyka", room: "B10", startTime: "10:45", endTime: "11:30", dayOfWeek: "Friday"), Lesson(name: "Godzina Wychowawcza", room: "C15", startTime: "11:40", endTime: "12:25", dayOfWeek: "Friday"), Lesson(name: "WF", room: "Sala Gimnastyczna", startTime: "12:35", endTime: "13:20", dayOfWeek: "Friday"), Lesson(name: "Biologia", room: "C7", startTime: "13:25", endTime: "14:10", dayOfWeek: "Friday"), ] #endif
3
0
617
Oct ’24
Unable to change UITabbar Background color in iOS 18 for ipad
I am trying to change UITabBar background color runtime as theme changed. It is already working in iOS 17 as I am updating UITabBar.appearance().barTintColor and tintColor But for iOS first i need to change because I don't want that new elevated tabbar so I create custom tabbar controller as described in https://stackoverflow.com/questions/78631030/how-to-disable-the-new-uitabbarcontroller-view-style-in-ipados-18 Accepted Answer by awulf. And by doing this, My tabbar looks same like Old and it is working in iPhone and ipad for iOS 16, iOS 17 and iOS 18 too. But the issue is that I am unable to change my tabbar background color. I have also checked this forum: https://forums.developer.apple.com/forums/thread/761056 But not able to change I have set below 3 properties but no effect let appearance = UITabBar.appearance() appearance.backgroundColor = appearance.barTintColor = appearance.tintColor = I have created CustomTabBarController in storyboard and all working fine Also the appearance changed only once per application lifecycle. It will change color by restarting the app then it will pick last selected theme and the colors are changed. but not able to change colors runtime I have also did below code for reloading purpose tabBar.setNeedsLayout() tabBar.setNeedsDisplay() But nothing work
0
1
976
Oct ’24
How to wrangle Sendable ReferenceFileDocument and SwiftUI
I recently circled back to a SwiftUI Document-based app to check on warnings, etc. with Xcode 16 and Swift 6 now released. I just found a suite a new errors that indicate that ReferenceFileDocument is now expected to be Sendable, which seems very off for it fundamentally being a reference type. I followed the general pattern from earlier sample code: Building Great Apps with SwiftUI, which goes from an Observable object to adding conformance to ReferenceFileDocument in an extension. But then the various stored properties on the class cause all manner of complaint - given that they're not, in fact, sendable. What is an expected pattern that leverages a final class for the ReferenceDocument, but maintains that Sendability? Or is there a way to "opt out" of Sendability for this protocol? I'm at a bit of a loss on the expectation that ReferenceFileDocument is sendable at all, but since it's marked as such, I'm guessing there's some path - I'm just not (yet) clear on how to accomodate that.
1
0
631
Oct ’24
grid and buttons
I would like to create 5 buttons in two rows the first row contains 3 buttons while the second 2 buttons button D should occupy the space occupied by the two buttons at the top A and B while as you can see from the attached image button D is as wide as the others on visualstudio c# I know how to do it but with Xcode Swift I can't find any documentation that can help me
Topic: UI Frameworks SubTopic: SwiftUI
4
0
433
Oct ’24
SwiftUI LongPressGesture does not work as expected in Xcode Version 16.0 (16A242) and iOS 18
When I copy and paste example code in apple developer documentation, LongPressGesture does not work as expected in Xcode Version 16.0 (16A242) and iOS 18. It seems updating(_:body:) method does not work when used with LongPressGesture. When I make a breakpoint in updating(_:body:) method and long press the blue circle on the screen of simulator(or device), it is expected to be caught in breakpoint or it is expected that color of circle turns from blue to red to green. However, it is not caught in breakpoint and never turns to red. Question of Stackoverflow is about same issue and I can not use onLongPressGesture method to implement required feature of my app. I submitted bug report via Feedback Assistant and my report's ID is FB15127375 and FB15173175. Please tell me if there is any investigation about this issue. Development environment: Xcode Version 16.0 (16A242), macOS 14.5 Run-time configuration: iOS 18.0
1
1
913
Oct ’24
NSApplicationDelegate open URLs only called after second drop
I want to make a simple droplet application. I've set the document types to include com.adobe.pdf files, and I am now receiving callbacks to the app delegate's application(_:open:) callback when I drop PDFs on the app icon… But not the first time. It doesn't matter how long I wait, or whether the app is already open—the first drop never triggers the callback. All subequent drops work as expected, and I get URLs to all the files dropped. What might be wrong? How can I debug this?
1
0
566
Oct ’24
How to permanently move Command+A focus from SideBar to DetailView?
My NavigationSplitView is very simple. The DetailView contains Table populated with data. SideBar populated with items that act as a filter for Table content. If I'm on the SideBar and hit Command-A, I never want to select everything in the sidebar. I always want to select all the content for a detail view. This is how Finder works. I tried to set List(...) { ... } .focusable(false) When I launch the application, Command-A works exactly as I would like. But when I select another "filter" in sidebar with the mouse, the List becomes focusable.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
0
0
261
Oct ’24
tvOS App support portrait display?
Hi, We are going to create a tvOS App with portrait display(HDMI screen will rotate 90 degree). It seems there is no rotate setting in tvOS18, neither Xcode provide relative support. As our investigation, we might need to rotate each UIKit component 90 degree by code to archive it. Is there any better suggestion? Thanks.
0
0
495
Oct ’24
What is the correct method for renaming a file in the app's container using the Swiftui 2.0 Document based template?
I've been using the new template for a document-based SwiftUI app. While you get a lot of file-management "for free" in the new template, as it stands in the iOS version users have to back out of the file to the file browser to change the filename. I want to create an opportunity for the user to rename the file while it is open. Here's a sample project focused on the issue: https://github.com/stevepvc/DocumentRenamer In the code, I've added to the template code a simple UI with a textfield for the user to enter a new name. When the user hits the "rename" button, the app checks to see if the URL with that name component is available, appending a suffix if necessary to create a target url. func getTargetURL() -> URL { &#9;&#9;let baseURL&#9;=&#9;self.fileurl.deletingLastPathComponent() &#9;&#9;print("filename: \(self.filename)") &#9;&#9;print("fileURL: \(self.fileurl)") &#9;&#9;print("BaseURL: \(baseURL)") &#9;&#9;var target = URL(fileURLWithPath: baseURL.path + "/\(filename).exampletext") &#9;&#9;var nameSuffix = 1 &#9;&#9; &#9;&#9;while (target as NSURL).checkPromisedItemIsReachableAndReturnError(nil) { &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;target = URL(fileURLWithPath: baseURL.path + "/\(filename)-\(nameSuffix).sermon") &#9;&#9;&#9;&#9;print("Checking: \(target)") &#9;&#9;&#9;&#9;nameSuffix += 1 &#9; } &#9;&#9;print("Available Target: \(target)") &#9;&#9;return target } It then attempts to rename the file, and this is when I am stuck. I have tried several methods, most recently the following: func changeFilename(){ &#9;&#9;let target&#9;= getTargetURL() &#9;&#9;var rv = URLResourceValues() &#9;&#9;let newFileName = target.deletingPathExtension().lastPathComponent &#9;&#9;rv.name = newFileName &#9;&#9;do { &#9;&#9;&#9;&#9;if fileurl.startAccessingSecurityScopedResource(){ &#9;&#9;&#9;&#9;&#9;&#9;try fileurl.setResourceValues(rv) &#9;&#9;&#9;&#9;&#9;&#9;fileurl.stopAccessingSecurityScopedResource() &#9;&#9;&#9;&#9;} &#9;&#9;} catch { &#9;&#9;&#9;&#9;print("Error:\(error)") &#9;&#9;} } But I keep getting the following error whenever I run the app on a device: Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “Untitled” in the folder “DocumentRenamer”." I have also tried this without the startAccessingSecurityScopedResource() check, and alternatively have tried creating a helper class as follows: class FileMover: NSObject { func moveFile(originalURL: URL, updatedURL:URL) -> Bool { &#9;&#9;let coordinator = NSFileCoordinator(filePresenter: nil) &#9;&#9;var writingError: NSError? = nil &#9;&#9;var success : Bool = true &#9;&#9;print("moving file") &#9;&#9;coordinator.coordinate(writingItemAt: originalURL, options: NSFileCoordinator.WritingOptions.forMoving, error: &writingError, byAccessor: { (coordinatedURL) in &#9;&#9;&#9;&#9;do { &#9;&#9;&#9;&#9;&#9;&#9;try FileManager.default.moveItem(at: coordinatedURL, to: updatedURL) &#9;&#9;&#9;&#9;&#9;&#9;success = true &#9;&#9;&#9;&#9;&#9;&#9;print("file moved") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} catch { &#9;&#9;&#9;&#9;&#9;&#9;print(error) &#9;&#9;&#9;&#9;&#9;&#9;success = false &#9;&#9;&#9;&#9;} &#9;&#9;}) return success &#9;&#9; } } But using this method locks up the app entirely. What is the correct method for renaming a file in the app's container, particularly using the new Swiftui Document based template?
3
3
3.5k
Oct ’24
Complication Widget using AccessoryWidgetGroup
I want to get which button in AccessoryWidgetGroup was pressed in the Watch App, but I can't. When I use Button(_:intent:), it works in Smart Stack, but it doesn't work in Complication Widget. Using widgetURL(_:) always gets the URL of the first button. Below is a snippet of the code I tried. All codes can be found here. struct MyWatchWidgetEntryView : View { var entry: Provider.Entry var body: some View { AccessoryWidgetGroup("Widget Group") { MyWatchWidgetEntryButton(intent: .init(id: "001", imageName: "star.fill")) MyWatchWidgetEntryButton(intent: .init(id: "002", imageName: "heart.fill")) MyWatchWidgetEntryButton(intent: .init(id: "003", imageName: "leaf.fill")) } } } //Button View private struct MyWatchWidgetEntryButton: View { @Environment(\.showsWidgetContainerBackground) var showsWidgetContainerBackground let intent: MyAppIntent var body: some View { Button(intent: intent) { ZStack { if showsWidgetContainerBackground { Color.black } else { AccessoryWidgetBackground() } VStack { Image(systemName: intent.imageName) .font(.headline) Text(intent.id) .font(.system(size: 10, weight: .bold)) } } } .buttonStyle(.plain) .widgetURL(URL(string: "widget://" + intent.id)) } } Does anyone know how to do this? Thank you.
4
0
791
Oct ’24
Split View DoubleColumn Mode
Hi, When SplitView is in detailsOnly and I swipe from left side the sidebar appears as popover above the details content, but when I try to open it manually by settings the columnVisibility to doubleColumn it pushes the details view and shows, so haw to make it appear as poorer ? Kind Regards
Topic: UI Frameworks SubTopic: SwiftUI
0
0
182
Oct ’24
Toolbar buttons in HStack merge with menu when .buttonStyle(…) is set
I do not understand what is happening here. In this example, the toolbar has three Button and one Menu inside an HStack, where the every button has a .buttonStyle(_:) set. ToolbarItem(placement: .bottomBar) { HStack { Button {} label: { Label("Foo", systemImage: "square") } .buttonStyle(.bordered) Button {} label: { Label("Bar", systemImage: "circle") } .buttonStyle(.borderless) Menu { Button {} label: { Label("One", systemImage: "figure.arms.open") } Button {} label: { Label("Two", systemImage: "figure.2.arms.open") } } label: { Label("Baz", systemImage: "star") } Button {} label: { Label("Quux", systemImage: "triangle") } .buttonStyle(.borderedProminent) } } Please note: the menu has a star icon. This causes only the menu button to appear, but with the first button's icon: If a single button have has its styles removed, the toolbar appears as expected (in this example all button styles are removed): This example uses the bottom bar, but also happens when using placement: .navigation, placement: .topBarLeading, placement: .topBarTrailing.
2
0
471
Oct ’24