Post

Replies

Boosts

Views

Activity

Reply to How can I change the background color of a focused item of a NSTableView?
Here is a solution i found: Remove the highlight style of your tableView like this: tableView.selectionHighlightStyle = .none Set different background in  tableViewSelectionDidChange method like this: func tableViewSelectionDidChange(_ notification: Notification) { guard let tableView = notification.object as? NSTableView else { return } guard tableView.selectedRow >= 0 else { for i in 0..<tableView.numberOfRows { guard let rowView = tableView.rowView(atRow: i, makeIfNecessary: false) else { return } rowView.backgroundColor = .clear } return } guard let rowView = tableView.rowView(atRow: tableView.selectedRow, makeIfNecessary: false) else { return } rowView.backgroundColor = .red.withAlphaComponent(0.1) } The full code can be found here.
Topic: UI Frameworks SubTopic: AppKit Tags:
3w
Reply to How can I change the background color of a focused item of a NSTableView?
Another thing that I could do – is change AccentColor in project's Assets.xcassets. But it does not address the issue from this topic, because setting a color with 0.10% opacity gives an unexpected result :( Here i set the same brown color, but set the slider at the very left (1%) in the Assets.xcassets making the color barely visible: As you can see instead of giving me a barely brown color it gave me almost white...
Topic: UI Frameworks SubTopic: AppKit Tags:
4w
Reply to How can I change the background color of a focused item of a NSTableView?
2: Replace property override with a method override in the MyCustomRowView from the code above: class MyCustomRowView: NSTableRowView { override func drawSelection(in dirtyRect: NSRect) { NSColor.brown.setFill() self.bounds.fill() } } It changes the background, but not the background we had initially. It completely ignores the interaction with the swipe action:
Topic: UI Frameworks SubTopic: AppKit Tags:
Mar ’25
Reply to How can I change the background color of a focused item of a NSTableView?
There are two things which can be done: 1: Add another tableView method into the Coordinator which looks like this: func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { return MyCustomRowView() } And MyCustomRowView would just have a isEmphasized property override: class MyCustomRowView: NSTableRowView { override var isEmphasized: Bool { set {} get { false } } } It can not give us any selection color we want, but it does change the color. To gray:
Topic: UI Frameworks SubTopic: AppKit Tags:
Mar ’25
Reply to How can I change the background color of a focused item of a NSTableView?
Here is a more simplified example of the app: import SwiftUI struct NSTableViewWrapper: NSViewRepresentable { class Coordinator: NSObject, NSTableViewDataSource, NSTableViewDelegate { var parent: NSTableViewWrapper init(parent: NSTableViewWrapper) { self.parent = parent } func numberOfRows(in tableView: NSTableView) -> Int { 1 } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { return NSTextField(labelWithString: "Item") } func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] { return [NSTableViewRowAction(style: .destructive, title: "Delete") { _, _ in }] } } func makeCoordinator() -> Coordinator { return Coordinator(parent: self) } func makeNSView(context: Context) -> NSScrollView { let scrollView = NSScrollView() let tableView = NSTableView() let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Column")) tableView.addTableColumn(column) tableView.delegate = context.coordinator tableView.dataSource = context.coordinator scrollView.documentView = tableView return scrollView } func updateNSView(_ nsView: NSScrollView, context: Context) { (nsView.documentView as? NSTableView)?.reloadData() } } struct ContentView: View { var body: some View { NSTableViewWrapper() } } And here is the demo:
Topic: UI Frameworks SubTopic: AppKit Tags:
Mar ’25
Reply to List does not move the view into focused element, when changing it with a keyboard
NSTableView from AppKit works just as expected, while List from SwiftUI does this weird "cropping". Here is the code for the NSTableView: import SwiftUI struct NSTableViewWrapper: NSViewRepresentable { class Coordinator: NSObject, NSTableViewDataSource, NSTableViewDelegate { var parent: NSTableViewWrapper init(parent: NSTableViewWrapper) { self.parent = parent } func numberOfRows(in tableView: NSTableView) -> Int { return parent.data.count } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("Cell"), owner: nil) as? NSTextField ?? NSTextField(labelWithString: "") cell.identifier = NSUserInterfaceItemIdentifier("Cell") cell.stringValue = parent.data[row] return cell } } var data: [String] func makeCoordinator() -> Coordinator { return Coordinator(parent: self) } func makeNSView(context: Context) -> NSScrollView { let scrollView = NSScrollView() let tableView = NSTableView() let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Column")) column.title = "Items" column.width = 200 tableView.addTableColumn(column) tableView.delegate = context.coordinator tableView.dataSource = context.coordinator tableView.headerView = nil tableView.rowHeight = 50 tableView.style = .plain scrollView.documentView = tableView scrollView.hasVerticalScroller = true return scrollView } func updateNSView(_ nsView: NSScrollView, context: Context) { (nsView.documentView as? NSTableView)?.reloadData() } } struct ContentView: View { let items = 0..<40 let itemsSteing = Array(0..<40).map(\.description) var body: some View { NSTableViewWrapper(data: itemsSteing) } } func createAppWindow() { let window = NSWindow( contentRect: .zero, styleMask: [.titled], backing: .buffered, defer: false ) window.title = "NSTableView from AppKit" window.contentViewController = NSHostingController(rootView: ContentView()) window.setContentSize(NSSize(width: 759, height: 300)) window.center() window.makeKeyAndOrderFront(nil) } class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ notification: Notification) { createAppWindow() } } let delegate = AppDelegate() NSApplication.shared.delegate = delegate NSApplication.shared.run()
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’25