Without resorting to NSViewRepresentable, is there a view or view modifier in SwiftUI that can create an NSComboButton on macOS?
NSComboButton was introduced in macOS 13 and is (relatively) new to AppKit:
Apple Developer - NSComboButton
I only require support on macOS for this control.
Note that this is not to be confused with NSComboBox, which is a completely different control.
SwiftUI
RSS for tagProvide 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
Context:
The SwiftUI @Query macro has an internal modelContext.
The ModelActor also has a modelContext, from which the data should be read/written.
Issue:
When writing to @Model data fetched with @Query macro using a ModelActor, it will crash in the most not-obvious ways.
Also, fetching @Model with ModelActor will result in errors in Swift 6 since @Model aren't sendable.
Problem to Solve:
- How to write a good amount of data to SwiftData/CoreData without blocking the UI thread?
Would the recommendation from the Apple team be that a large amount of data should be read/written with ModelActor and a small amount should be done with the @Query's internal modelContext ?
I am working with a watchOS app in SwiftUI, and I am using the following code to dial a phone number from the watch:
var number = "123456789"
if let telURL = URL(string: "tel:\(number)") {
let wkExtension = WKExtension.shared()
wkExtension.openSystemURL(telURL)
}
The issue is that when I try to dial a number starting with a * (asterisk) or # (hash), it doesn't work. When dialing a regular number, it works fine.
Is there any way to get this to work?
I am writing a SwiftUI based app, and errors can occur anywhere. I've got a function that logs the error.
But it would be nice to be able to call an Alert Msg, no matter where I am, then gracefully exits the app.
Sure I can right the alert into every view, but that seems ridiculously unnecessary.
Am I missing something?
I'm currently working on a project to integrate some SwiftUI components into an existing AppKit application. The application makes extensive use of NSViewControllers. I can easily bridge between AppKit and SwiftUI using a view model that conforms to ObservableObject and is shared between the NSViewController and the SwiftUI View. But it's kind of tedious creating a view model for every view.
Is it "safe" and "acceptable" for the NSViewController to "hold on" to the SwiftUI View that it creates and then access its @State or @StateObject properties?
The lifecycle of DetailsView, a SwiftUI View, isn't clear to me when viewed through the lens of an NSViewController. Consider the following:
import AppKit
import SwiftUI
struct DetailsView: View {
@State var details: String = ""
var body: some View {
Text(details)
}
}
final class ViewController: NSViewController {
private let detailsView: DetailsView
init() {
self.detailsView = DetailsView()
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
view.addSubview(NSHostingView(rootView: detailsView))
}
func updateDetails(_ details: String) {
// Is this 'safe' and 'acceptable'?
self.detailsView.details = details
}
}
Is the view controller guaranteed to always be updating the correct @State property or is there a chance that the view controller's reference to it somehow becomes stale because of a SwiftUI update?
I am getting memory leak when using .onSubmit view modifier on TextField.
Here is a simplified reproducible example:
struct ListView: View {
var body: some View {
NavigationStack {
List(0..<100) { i in
NavigationLink("Select \(i)", value: i)
}
.navigationDestination(for: Int.self) { selection in
DetailView()
.navigationTitle("Page: \(selection)")
}
}
}
}
struct DetailView: View {
@State private var viewModel = DetailViewModel()
var body: some View {
TextField(
"",
text: $viewModel.text,
prompt: Text("Type in")
)
.padding()
.onSubmit {
print(viewModel.text)
}
}
}
@Observable
final class DetailViewModel {
var text: String = ""
deinit {
print("Deinit \(Self.self)")
}
}
Navigate to DetailView by selecting a row on ListView page and start typing in TextField, hit the submit button on keyboard and this DetailView with DetailViewModel will be leaked after navigating back to ListView, deinit will not be called.
Commenting out .onSubmit {} part fixes the leak.
What I observed also, is that once I open the same page or other, It will create new DetailView instance with it's view model and previously leaked one will be released, possible indicating that system somehow holds the last active TextField until new one has become active.
I tried calling UIApplication.shared.resignFirstResponder() in onDissapear{} but this does not fix the leak.
Only way the leak is fixed, is by using deprecated TextField initializer:
init(_ titleKey: LocalizedStringKey, text: Binding<String>, onCommit: @escaping () -> Void)
where onCommit is essentially doing the same as onSubmit.
I Am interested in coding, and built my fist app that is an app that has a picture of Niagara Falls with corner radius of 10, But, every time I start the build, it says: Thread 1: EXC_BAD_ACCESS (code=2, address=0x16b123f20) not sure what to do now.
For my macOS app, I'm trying to change the mouse cursor to a pointing hand while hovering over a specific view. However, when the view is scaled with an animation triggered by hovering (using .scaleEffect() and .animation()), the cursor doesn't change as expected. Is there any workaround to fix this?
This is a sample code:
struct ContentView: View {
@State private var hovering = false
var body: some View {
VStack {
Text("Hover me")
.padding()
.background(hovering ? Color.blue : Color.gray)
.scaleEffect(hovering ? 1.2 : 1.0)
.animation(.linear(duration: 0.2), value: hovering)
.onHover { hovering in
self.hovering = hovering
if hovering {
NSCursor.pointingHand.push()
} else {
NSCursor.pop()
}
}
}
.frame(width: 200, height: 200)
}
}
This is how it works:
As you can see, when the pointer enters the view, the cursor changes momentarily before reverting back to the arrow icon.
I also tried using NSTrackingArea with an NSView placed over the view, but it did not solve the issue. It might be that the combination of .scaleEffect() and .animation() is causing a forced cursor reset (possibly related to the use of NSWindow.disableCursorRects() or something similar). However, I'm not entirely sure.
Any insights or suggestions would be greatly appreciated. Thanks!
All of a sudden, after iOS 18.4 was released, I am having tons of navigation problems in my app in production. Buttons navigating to empty pages, views seeming to 'freeze', top navigation bar mismatched with the content of the page. It seems that iOS 18.4 broke a critical piece of UIKit + SwiftUI bridging functionality that my project relies on.
** Originally posted in 'Core OS' topic but realized 'UI Frameworks > General' made more sense. My bad. **
My application is written with both UIKit and SwiftUI components. Here is a breakdown of my setup:
UIApplicationDelegate >
UIWindow >
rootViewController of window is a UITabBarController >
each tab is a UINavigationController
rootViewController of nav controller is a UIHostingController >
rootView of the hosting controller is a SwiftUI View
In my SwiftUI views, I have been using NavigationLink for horizontal 'push' style navigation in my SwiftUI views. I do not use NavigationView, I only rely on the bridging capabilities of UINavigationController to action on my NavigationLinks. This has never been an issue, until iOS 18.4 was released. Now, when running iOS 18.4, I am having all sorts of unexpected behavior in the UI. I will break down 2 of these use cases here:
Use case A:
In one of my SwiftUI views, I have a ForEach for which each element's view is a NavigationLink. This is using the NavigationLink(_ destination:,label:) initializer. Navigating forward from here works/looks normal.
However, once I try to navigate backward from that destination (tap the 'Back' button in top left), the view goes blank and the navigation bar at the top of the page (which is maintained by the UINavigationController instance) does not change. If I call popToRootViewController on that nav controller, the navigation bar at the top of the page returns to its normal state, but the view is still blank.
It is not until after I have called popToRootViewController, and then navigate to a different tab of the UITabBarController and return to the initial tab, does the SwiftuI content view (the one with the ForEach) finally redraw and the view hierarchy is restored.
Here is a warning that is logged in the console when I tap the 'Back ' button:
Top view controller's view unexpectedly not in window for navigation transition. Skipping layout. nav = <UINavigationController: 0x1110bbe00>, topVC = <TtGC7SwiftUI19UIHostingControllerV5MyApp10MyPage: 0x106814e00>
EDIT: If I replace the NavigationLink with a call to UINavigationController.pushViewController, I am still seeing the exact same behavior. Pressing back button makes the view empty > need to pop to root view controller and switch tabs in order to restore the view.
Use case B
Another instance of this issue happens whenever I try to use a NavigationLink inside of a view that itself was the destination of a NavigationLink in its parent view (i.e.: Root view > detail view > sub-detail view). For example, take the detail view destination in use case A. I have tapped a NavigationLink from the ForEach and landed on the detail view. Again, so far things work/look normal. Now, if I tap on another NavigationLink from that detail view, the view does not transition to the new page. The top navigation bar does transition, and shows the title and actions associated with this second destination. However, the view of this second destination is not displayed.
It is worth noting that the same warning I mentioned above is also logged when I tap the NavigationLink to navigate to this second destination.
Top view controller's view unexpectedly not in window for navigation transition. Skipping layout. nav = <UINavigationController: 0x109859400>, topVC = <TtGC7SwiftUI19UIHostingControllerVVS_19BridgedPresentation8RootView: 0x300ab8000>
Strangely, if I switch to a different tab of the UITabBarController and back to the initial tab, this second destination's view is successfully rendered. It seems that switching tabs in this UITabBarController is calling something in either SwiftUI or UIKit that is redrawing my views.
Conclusion
This is a serious issue with UIKit + SwiftUI bridging support. I have never had problems like this until devices started running iOS 18.4, and there is nothing in the iOS 18.4 changelog that suggests this was an intentional change. All of a sudden, after updating to the latest iOS version, my app is totally broken.
I want to be clear that I'm not using deprecated NavigationLink methods in these instances. My app's minimum deployment target is iOS 16.
I know that there are more modern navigation APIs like navigation stack, etc. I am looking for answers about my use case: whether it is officially unsupported as of iOS 18.4, whether this setup should be supported and this is indeed some sort of bug in iOS, or anything in-between. I'm happy to provide formatted code if needed for discussion purposes. This is about my entire app's view hierarchy so there are a lot of disparate lines of code that make up this problem.
All of a sudden, after iOS 18.4 was released, I am having tons of navigation problems in my app in production. Buttons navigating to empty pages, views seeming to 'freeze', top navigation bar mismatched with the content of the page. It seems that iOS 18.4 broke a critical piece of UIKit + SwiftUI bridging functionality that my project relies on.
My application is written with both UIKit and SwiftUI components. Here is a breakdown of my setup:
UIApplicationDelegate >
UIWindow >
rootViewController of window is a UITabBarController >
each tab is a UINavigationController
rootViewController of nav controller is a UIHostingController >
rootView of the hosting controller is a SwiftUI View
In my SwiftUI views, I have been using NavigationLink for horizontal 'push' style navigation in my SwiftUI views. I do not use NavigationView, I only rely on the bridging capabilities of UINavigationController to action on my NavigationLinks. This has never been an issue, until iOS 18.4 was released. Now, when running iOS 18.4, I am having all sorts of unexpected behavior in the UI. I will break down 2 of these use cases here:
Use case A:
In one of my SwiftUI views, I have a ForEach for which each element's view is a NavigationLink. This is using the NavigationLink(_ destination:,label:) initializer. Navigating forward from here works/looks normal.
However, once I try to navigate backward from that destination (tap the 'Back' button in top left), the view goes blank and the navigation bar at the top of the page (which is maintained by the UINavigationController instance) does not change. If I call popToRootViewController on that nav controller, the navigation bar at the top of the page returns to its normal state, but the view is still blank.
It is not until after I have called popToRootViewController, and then navigate to a different tab of the UITabBarController and return to the initial tab, does the SwiftuI content view (the one with the ForEach) finally redraw and the view hierarchy is restored.
Here is a warning that is logged in the console when I tap the 'Back ' button:
Top view controller's view unexpectedly not in window for navigation transition. Skipping layout. nav = <UINavigationController: 0x1110bbe00>, topVC = <TtGC7SwiftUI19UIHostingControllerV5MyApp10MyPage: 0x106814e00>
EDIT: If I replace the NavigationLink with a call to UINavigationController.pushViewController, I am still seeing the exact same behavior. Pressing back button makes the view empty > need to pop to root view controller and switch tabs in order to restore the view.
Use case B
Another instance of this issue happens whenever I try to use a NavigationLink inside of a view that itself was the destination of a NavigationLink in its parent view (i.e.: Root view > detail view > sub-detail view). For example, take the detail view destination in use case A. I have tapped a NavigationLink from the ForEach and landed on the detail view. Again, so far things work/look normal. Now, if I tap on another NavigationLink from that detail view, the view does not transition to the new page. The top navigation bar does transition, and shows the title and actions associated with this second destination. However, the view of this second destination is not displayed.
It is worth noting that the same warning I mentioned above is also logged when I tap the NavigationLink to navigate to this second destination.
Top view controller's view unexpectedly not in window for navigation transition. Skipping layout. nav = <UINavigationController: 0x109859400>, topVC = <TtGC7SwiftUI19UIHostingControllerVVS_19BridgedPresentation8RootView: 0x300ab8000>
Strangely, if I switch to a different tab of the UITabBarController and back to the initial tab, this second destination's view is successfully rendered. It seems that switching tabs in this UITabBarController is calling something in either SwiftUI or UIKit that is redrawing my views.
Conclusion
This is a serious issue with UIKit + SwiftUI bridging support. I have never had problems like this until devices started running iOS 18.4, and there is nothing in the iOS 18.4 changelog that suggests this was an intentional change. All of a sudden, after updating to the latest iOS version, my app is totally broken.
I want to be clear that I'm not using deprecated NavigationLink methods in these instances. My app's minimum deployment target is iOS 16.
I know that there are more modern navigation APIs like navigation stack, etc. I am looking for answers about my use case: whether it is officially unsupported as of iOS 18.4, whether this setup should be supported and this is indeed some sort of bug in iOS, or anything in-between. I'm happy to provide formatted code if needed for discussion purposes. This is about my entire app's view hierarchy so there are a lot of disparate lines of code that make up this problem.
Hi everyone,
I'm facing an issue where I cannot write a file to a shared App Group container in my tvOS app when running on a real device. My code works perfectly on the simulator, but fails on a physical device with a permissions error. I’ve set up an App Group with a custom identifier (e.g., group.<my.identifier>), and it’s correctly configured in the Capabilities section of Xcode for both my main app and widget targets.
Here’s the code I’m using to save a test file:
func saveTestFile() {
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.<my.identifier>") else {
print("Couldn't access the Group URL.")
return
}
let containerURL = groupURL.appendingPathComponent("Library", isDirectory: true)
if FileManager.default.isWritableFile(atPath: containerURL.path) {
print("Directory IS writable")
} else {
print("Directory IS NOT writable")
}
let fileURL = containerURL.appendingPathComponent("test.txt")
let content = "Hello App Group!"
do {
try content.write(to: fileURL, atomically: true, encoding: .utf8)
print("File test.txt is saved at: \(fileURL.path)")
} catch {
print("Error while saving the file: \(error)")
}
}
Console:
Directory IS NOT writable
Error while saving the file: Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “test.txt” in the folder “”." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup//Library/test.txt, NSURL=file:///private/var/mobile/Containers/Shared/AppGroup//Library/test.txt, NSUnderlyingError=0x14387fbe0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
I’ve tried saving the file in different subdirectories within the App Group container:
Directly in groupURL (root of the container).
In groupURL.appendingPathComponent("Library").
In groupURL.appendingPathComponent("Caches").
Do you have any ideas what is the problem?
Thanks in advance for any help!
Hi everyone,
I'm working on a tvOS app using SwiftUI, and I want to disable the focus effect (the default focus glow/bounce) on a specific Button. According to the documentation:
/// - Parameter disabled: A Boolean value that determines whether this view
/// can display focus effects.
/// - Returns: A view that controls whether focus effects can be displayed
/// in this view.
I used .focusEffectDisabled(true) on the Button, expecting the focus style to be completely disabled for that view. However, this doesn’t seem to have any effect in my tvOS 17+ app – the button still shows the default focus visual effect when focused.
Here’s a simplified example:
Button("Click Me") {
// action
}
.focusEffectDisabled(true)
This still shows the bounce/glow focus effect. Am I missing something, or is this a bug?
Has anyone managed to fully disable the focus effect for a view (especially Button) in tvOS using SwiftUI? Any workarounds or additional modifiers I should apply?
Thanks in advance!
Consider the following example of a List view containing sections of rows.
import SwiftUI
struct ListSectionView: View {
@State private var sectionHeaders = ["section1", "section2", "section3"]
var body: some View {
List {
ForEach(sectionHeaders, id: \.self) { sectionHeader in
Section(header: Text(sectionHeader)) {
Text("1")
Text("2")
Text("3")
}
}
.onMove { indices, newOffset in
// ...
}
}
}
}
I would like to reorder the sections within the list by dragging the respective section header to its new position in the list - similar to moving individual rows via onMove-dragging but with the sections instead of the rows.
The above approach does not work. It "activates" moving the rows and then the .onMove code acts on those. The sections themselves are not moved.
How can I move/reorder the sections within the list?
Thanks.
I have been working on a feature, where I have a List in SwiftUI with previous and next data loading, user can scroll up and down to load previous/next page data.
Recently, I faced one accessibility issue while testing voice-over, when user lands on the listing screen and swipe across the screen from navigation and when focus comes on list it should highlight the first item visible.
But when user swipes back:
Should it load the previous data and announce the previous item or it should go back to the navigation items?
If it loads the previous item, what if the user wants to go to the navigation to switch to other actions and vice-versa?
Did anyone come across this kind of issue? What can be the standard expected behavior in this case if list has both previous and next page scroll?
I different tried gestures https://support.apple.com/en-in/guide/iphone/iph3e2e2281/ios, but it isn't working
Take a look at this simple code:
import Cocoa
import SwiftUI
struct DemoView: View {
var body: some View {
Text("Click me!")
.onTapGesture {
print("Clicked")
}
}
}
class FlippedView: NSView {
override var isFlipped: Bool {
return true
}
}
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = NSStackView()
stackView.orientation = .vertical
stackView.alignment = .leading
stackView.spacing = 0
stackView.translatesAutoresizingMaskIntoConstraints = false
let hostView = NSHostingView(rootView: DemoView())
stackView.addArrangedSubview(hostView)
let scrollView = NSScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
let flippedView = FlippedView()
flippedView.addSubview(stackView)
scrollView.documentView = flippedView
view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
}
I need my scroll view to start at the very top, so i put it inside a flipped document view.
But now .onTapGesture does not fire.
Hi, Im new to SwiftUI and Im trying to implement some drag and drop functionality for some tabs in my application.
Im using .draggable(_) and .dropDestination for this and the issue I have is that as I drag the view, the mouse cursor changes to the copy cursor with the green plus sign and I don't like it but I can't figure out how to avoid it.
Any help would be appreciated.
If you try to add a graph for a function in Apple Notes you can see that numbers marking coordinates are positioned along the axes (see screenshot 1).
But when I am making my own plot view with Swift Charts I don't see that option. Marks for X axis are positioned at the bottom, and marks for Y axis are positioned to the right. I don't see an API that can configure them to be shown along the axes.
Is there something that I am missing? Or is Apple just using some private API for that?
I could make a custom overlay to display these marks, but then I will have to adjust them while zooming myself, which can be problematic.
I have a DocumentGroup working with a FileDocument, and that's fine.
However, when someone creates a new document I want them to have to immediately save it. This is the behavior on ipadOS and iOS from what I can understand (you select where before the file is created).
There seems to be no way to do this on macOS?
I basically want to have someone:
create a new document
enter some basic data
hit "create" which saves the file
then lets the user start editing it
(1), (2), and (4) are done and fairly trivial.
(3) seems impossible, though...?
This really only needs to support macOS but any pointers would be appreciated.
Hey,
We're loading data from Core Data, and for some reason an error is thrown. This is happening extremely rarely and we haven't been able to reproduce it.
The error thrown has the following description:
Åtgärden kunde inte slutföras. (ScreenGenieCore.EnrolledView.(unknown context at $10087af4c).EnrolledError fel 0.)
It is occurring in an app written in SwiftUI when the user taps a button. The managed object context is initiated in app init and provided to the view using the @environment modifier. So the viewContext should always exist. Still it throws an error saying unknown context ....
Any guidance or possible things to investigate would be much appreciated.
Given a SwiftUI sheet with presentation detent(s):
struct ContentView: View {
@State var isSheetOpen = false
var body: some View {
Button("Open Sheet") {
isSheetOpen = true
}
.sheet(isPresented: $isSheetOpen) {
Color.black
.presentationDetents([.medium])
}
}
}
If you:
open the sheet
close it and rapidly open it again
the second presentation will ignore the detent and open a full screen sheet instead.
Xcode 16.3, iOS 18.4
Bug report FB17115890