Post

Replies

Boosts

Views

Activity

How to customize buttons in a SwiftUI view toolbar
I have the following test code import SwiftUI struct ContentView: View { @State private var field1 = "" @State private var field2 = "" @State private var field3 = "" var body: some View { VStack { Form { Section(header: Text("Input Fields")) { TextField("Field 1", text: $field1) TextField("Field 2", text: $field2) TextField("Field 3", text: $field3) } } List { ForEach(1..<6) { index in Text("Item \(index)") } } .listStyle(InsetListStyle()) } .navigationTitle("Form and List View") .toolbar { ToolbarItem(placement: .cancellationAction) { Button("Dismiss") { // Handle dismiss action } .foregroundStyle(.red) // } ToolbarItem(placement: .primaryAction) { Button("Done") { // Handle done action } .foregroundStyle(.green) // } } } } I can't customize the color of the Buttons. Besides .foreground modifier I have tried ..foregroundColor(.red) and .foregroundColor(.green) to no avail. How can I customize the buttons in the toolbar for both macOS and IOS
3
0
1k
Aug ’23
Swiftui Table statement conditional table columns
Is it possible to have conditional table columns for a swifui Table statement? Like for this code TableColumn("Image") { artPiece in if let imageData = artPiece.artImage.first, let image = UIImage(data: imageData!) { Image(uiImage: image) .resizable() .frame(width: 50, height: 50) } else { Image(systemName: "photo") .resizable() .frame(width: 50, height: 50) } } .customizationID("Image") TableColumn("Name", value: \.artName) .customizationID("Name") TableColumn ("Art ID", value: \.artPieceID) { artPiece in Text(String(artPiece.artPieceID)) } .customizationID("Art ID") have a conditional TableColumn for this part of my SWIFTDATA model var artDefinedFields: [ArtDefinedFields] = [] or if I change the variable string array to this var artDefinedFields: [ArtDefinedFields] = Array(repeating: ArtDefinedFields(), count: 10), initialize the array with "None" and only create a TableColumn when there is aArtDeginedFields value other than "None"
3
0
646
Aug ’24
How to get control from a combo box selection?
Using a combo box after a selection I want to get control to take some actions based on the selection after press tab or return in the field. I have tried a few controls like the one below: // textShouldEndEditing     func control(_ control: NSControl, textShouldEndEditing fieldEditor: NSText) - Bool {         print(#function,control.tag) But the above control only gets control if the field is edited. I want control once tab, an item selected or return is pressed while on the filed. How is this done?
2
0
581
Mar ’21
How to save an Image to NsData object and then retrieve.
My environment is as follows: Xcode 12, macOS BS developing a macOS app. I have an IBOutlet from a IB window as @IBOutlet weak var imageView: NSImageView! So, I want to store imageView to a NSData object and then save the NSData object in my UserDefaults. Once I saved the Image in my UserDefaults I need to be able to decode or retrieve the image for use in other parts of the app. How is this done? Also, I have seen some cautions about converting an Image to NSData object and retrieving. It seems the retrieved image may not be 100% as it was originally. Is this true and if so why? Thanks
2
0
1.2k
Jun ’21
How to populate an array within a Class Instance?
I have the following code: class ChartSeriesRow: ObservableObject {     var id = UUID()     var chartCategory: String     struct ChartSeriesData {         var chartEntry: [ChartData]     }     var chartSeries : ChartSeriesData     struct ChartData: Codable, Identifiable, Hashable {         var id = UUID()         let chartMonth: String         let chartSales: String     }     init() {         chartCategory = ""         chartSeries = ChartSeriesData(chartEntry: [ChartData(chartMonth: "01", chartSales: "10.0") ] )     } } How do I populate ChartSeriesData? I thought I could just add the following: However coding this way there is no append method. extension ChartSeriesRow {     func addRow(chartRow: ChartSeriesData){          ChartSeriesRow.ChartSeriesData.chartEntry.append(contentsOf: chartRow)     } }
2
0
827
Nov ’22
TableView not populating?
I have the following code in a viewController: extension InvoicesDetailController: NSTableViewDelegate {     fileprivate enum CellIdentifiers {         static let lineItemLabelCell = "lineItemLabelCell"         static let lineItemDescCell = "lineItemDescCell"         static let lineItemAmountCell = "lineItemAmountCell"         static let lineItemQtyCell = "lineItemQtyCell"     }     fileprivate enum fieldIdentifiers {         static let lineItemLabel = "lineItemLabel"         static let lineItemDesc = "lineItemDesc"         static let lineItemAmount = "lineItemAmount"         static let lineItemQty = "lineItemQty"     }   func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) - NSView? {         let currentItem = invoiceItems[row]         var text: String = ""         var cellIdentifier: String = ""         let currentColumn = tableColumn?.identifier.rawValue         if currentColumn == fieldIdentifiers.lineItemLabel {             text = currentItem.lineItemLabel!             cellIdentifier = CellIdentifiers.lineItemLabelCell         }         if currentColumn == fieldIdentifiers.lineItemDesc {             text = currentItem.lineItemDesc!             cellIdentifier = CellIdentifiers.lineItemDescCell         }         if currentColumn == fieldIdentifiers.lineItemAmount {             text = currentItem.lineItemDesc!             cellIdentifier = CellIdentifiers.lineItemAmountCell         }         if currentColumn == fieldIdentifiers.lineItemQty {             text = currentItem.lineItemQty!             cellIdentifier = CellIdentifiers.lineItemQtyCell         }         if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: self) as? NSTableCellView {             print(cell.textField?.stringValue)             cell.textField?.stringValue = text             print(text, cell.textField?.stringValue)             return cell         }         return nil     } I have used this code (similar code in other places and it works fine, however in this case the tableView Cells are not being populated. When I look at this code using debugger the line if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: cellIdentifier), owner: self) as? NSTableCellView always sets cell to nil? Since I don't see anything wrong with code I suspect there is an issue with the IB? Any ideas how to fix or how to debug further?
1
0
578
Mar ’21
How to add headers programmatically to NSTableView
I have created programmatically a NSTableView with a couple of columns, this table view has 3 columns and I want to set headers for the columns to accomplish the same thing as you get when you selected headers in IB. I looked for some examples, found a few things but not really work for me. How is this done. It seems like this should be easy to code. This is an example of one of the examples I found. let headerCell = CustomTableHeaderCell.init(textCell: "123123") // let font = NSFont(name: "Arial", size: 22.0) // headerCell.stringValue = "firstname" // headerCell.font = font self.tableView.tableColumns[0].headerCell = headerCell But CustomTableHeaderCell is not found. How is this done?
Topic: UI Frameworks SubTopic: AppKit Tags:
1
0
1.1k
Aug ’22
macOS TableView dynamic row height?
Environment is macOS Monterey Xcode 13. Is there a way to change the behavior of a macOS NSTableView to have the row height change based on the contents of a column. As an example when populating the column data and the width of the content exceeds the width of the column to have the content wrap to an additional line, which of course the row height would have to change then as well. If so, how can this be done? Thanks
1
0
1.3k
Aug ’22
NSGraphicsContext.current expression is ambiguous without more context
I am using the code below to learn more about drawing but I am getting an ambiguous error on the line not sure how to resolve this. Please advise how to code this. Thanks let context = NSGraphicsContext.current()?.cgContext  override func draw(_ dirtyRect: NSRect) {         super.draw(dirtyRect)        ** let context = NSGraphicsContext.current()?.cgContext**         let length = CGPoint(x: 100, y: 100)         let p1 = CGPoint(x: 200, y: 200)         let shape = "square"         context!.beginPath()         context!.move(to: p1)         if shape == "line" {             let pointEnd = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             context!.addLine(to: pointEnd)         } else if shape == "square" {             let p2 = CGPoint(x: p1.x + length.x, y: p1.y)             let p3 = CGPoint(x: p1.x + length.x, y: p1.y + length.y)             let p4 = CGPoint(x: p1.x, y: p1.y + length.y)             context!.addLine(to: p2)             context!.addLine(to: p3)             context!.addLine(to: p4)             context!.addLine(to: p1)         }                 context!.setStrokeColor(red: 1, green: 1, blue: 1, alpha: 1.0)         context!.setFillColor(red: 0, green: 1, blue: 0, alpha: 1)         context!.setLineWidth(2.0)         context!.strokePath()                 let textColor = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 1.0)         let textColorB = NSColor(calibratedRed: 1, green: 1, blue: 1, alpha: 0.0)         let rect = CGRect(x: 200, y: 200, width: 30, height: 130)         let paragraphStyle = NSMutableParagraphStyle()         paragraphStyle.alignment = .center         let attr = [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.foregroundColor: textColor, NSAttributedString.Key.backgroundColor: textColorB, NSAttributedString.Key.font:NSFont.init(name: "HelveticaNeue-Thin", size: 14)]               let q: NSString = "hello, world"         let center = CGPoint(x: 0, y: 0)         q.draw(at: center, withAttributes: attr as [NSAttributedString.Key : Any])     } }
1
0
845
Aug ’22