Post

Replies

Boosts

Views

Activity

Problem with alignment guides
I am creating a user interface for a working program. I am able to lay things out with offsets but am trying as a learning experience to do the same with alignment guides. For the section heading labeled "Group 1" I am able to get the layout I want. What I cannot get to work are alignment guides for the associated rounded rectangle. It appears that the alignment guides with the rounded rectangle only affect the contents i.e. the text "Data Table". Below is a picture of the code output and the code. How, using alignment guides, can move the top rounded rectangle? To keep the picture small I just copied the left side of the output window. The top rounded rectangle is approximately centered in this window. struct ContentView: View { var body: some View { NavigationStack { Text("Group1") .font(Font.custom("Arial", size: 16)) .alignmentGuide(HorizontalAlignment.center, computeValue: { d in print("\(d[HorizontalAlignment.leading])") // print leading = 0.0 // print center = 27 // print trailing = 54 return d[HorizontalAlignment.leading] + 255 }) ZStack (alignment: Alignment(horizontal: .center, vertical: .center)) { RoundedRectangle(cornerRadius: 10) .fill(.white) .alignmentGuide(HorizontalAlignment.center, computeValue: { d in return d[HorizontalAlignment.leading] + 45 }) VStack (alignment: HorizontalAlignment.leading) { NavigationLink(value: "Table1") { Text("Data Table") } .font(Font.custom("Arial", size: 14)) .buttonStyle(.link) .underline() .focusable(false) } // end vstack } // end zStack .frame(width: 200, height: 60) Text("Group2") .font(Font.custom("Arial", size: 16)) .frame(width: 600, height: 20, alignment: .leading) .padding(.leading, 35) .padding(.top, 20) .offset(x: 30) ZStack { RoundedRectangle(cornerSize: CGSize(width: 200, height: 60)) .stroke(Color(.clear), lineWidth: 1) .frame(width: 200, height: 60, alignment: .leading) .background(.white) .cornerRadius(10) .offset(x: -160) VStack () { NavigationLink(value: "Table2") { Text("Data Table") } .font(Font.custom("Arial", size: 14)) .buttonStyle(.link) .underline() .padding(.leading, 8) .frame(width: 200, height: 20, alignment: .leading) .offset(x: -160) .focusable(false) } } Text("Group3") .font(Font.custom("Arial", size: 16)) .frame(width: 600, height: 20, alignment: .leading) .padding(.leading, 35) .padding(.top, 20) .offset(x: 30) ZStack { RoundedRectangle(cornerSize: CGSize(width: 200, height: 60)) .stroke(Color(.clear), lineWidth: 1) .frame(width: 200, height: 60, alignment: .leading) .background(.white) .cornerRadius(10) .offset(x: -160) VStack () { NavigationLink(value: "Table3") { Text("Data Table") } .font(Font.custom("Arial", size: 14)) .buttonStyle(.link) .underline() .padding(.leading, 8) .frame(width: 200, height: 20, alignment: .leading) .offset(x: -160) .focusable(false) } } .padding(.bottom, 20) .navigationDestination(for: String.self) { command in switch command { case "Table1": HStack { CustomTableView(fundName: "Table1", numYears: 1) .frame(width: 320, height: 345, alignment: .center) .padding(.leading, 120) .padding(.trailing, 160) } case "Table2": HStack { CustomTableView(fundName: "Table2", numYears: 1) .frame(width: 320, height: 345, alignment: .center) .padding(.leading, 120) .padding(.trailing, 160) } case "Table3": HStack { CustomTableView(fundName: "Table3", numYears: 1) .frame(width: 320, height: 345, alignment: .center) .padding(.leading, 120) .padding(.trailing, 160) } default: let _ = print("problem in main view switch statement") } // end switch } // end navigation destination } .frame(width: 600, height: 400, alignment: .trailing) } }
1
0
1.1k
Sep ’23
Is it possible to call openWindow() in a navigationDestination in a NavigationStack?
I have a program that uses 3 different windows, each in their own window group. In a button I can use openWindow() to call another window and it works perfectly. I watched the video at the link below and now am trying to convert my program to use a navigation stack instead of buttons. I got things to work using the code listed at the link with a few changes but am unable to get the navigation destination to use openWindow. I attempted to create a view builder but could not get it to work. After some additional research I am starting to think that the navigation destination expects to use the same window as the root view not a separate window/view and therefore, navigation destination wants just a view. But I am unsure. If someone could provide some guidance it would be appreciated. https://developer.apple.com/videos/play/wwdc2022/10054/ struct ContentView: View { @Environment (\.openWindow) private var openWindow @StateObject private var dataModel = DataModel() var body: some View { VStack(alignment: .center) { NavigationStack() { List(Sections.allCases) { section in Section(section.localizedName) { ForEach(dataModel.stocks(in: section)) { stock in NavigationLink(stock.listingName, value: stock) .font(Font.custom("Arial", size: 16)) } } // end section } // end list .scrollContentBackground(.hidden) .background(Color.mint) .navigationTitle("Stocks") .navigationDestination(for: StockData.self) { stock in Text("\(stock.listingName) - \(stock.name)") // GenericTableView(content: openWindow(id: "table", value: stock.name)) // Cannot convert value of type '()' to expected argument type '() -> ContentView' } } // end navigation stack } // end v stack } } struct GenericTableView<Content:View>: View { let content: Content init(@ViewBuilder content: () -> Content) { self.content = content() } var body: some View { content } }
1
0
730
Oct ’23
How can I set the position of an app window on the screen for macOS with Swiftui?
I am trying to determine how to set the initial position of an app window for macOS. I have tried using NSViewRepresentable and .setFrameTopLeftPoint with no success. Is my syntax incorrect to achieve the desired results or should I be looking at some other method? struct WindowAccessor: NSViewRepresentable { @Binding var window: NSWindow? func makeNSView(context: Context) -> NSView { let view = NSView() view.window?.setFrameTopLeftPoint(CGPoint(x: 600, y: 400)) DispatchQueue.main.async { self.window = view.window } return view } func updateNSView(_ nsView: NSView, context: Context) {} } and var body: some Scene { WindowGroup { ContentView() .background(WindowAccessor(window: $window)) }
0
0
664
Nov ’21
How do I configure the headings in a SwiftUI table.
Hello, The following code displays the correct data but I am unable to figure out how to format the tables headings , including font size and alignment. Any help in pointing me in the right direction will be appreciated. Chris struct SummaryTable: View { private var localArray: [TradingDayPrices] init(passedInArray: [TradingDayPrices]) { self.localArray = passedInArray } var body: some View { let funds: [Fund] = GetSummaryTable(localArray: localArray) Table(funds) { TableColumn("Index Fund") { value in Text(value.name) .padding(.vertical, 2) } .width(90) TableColumn("Shares") { value in HStack { Spacer() Text(value.shares) Spacer() } } .width(90) TableColumn("Share Price") { value in HStack { Spacer() Text(value.sharePrice) Spacer() } } .width(90) TableColumn("Total") { value in HStack { Spacer() Text(value.total) } } .width(110) TableColumn("One Year Gain") { value in HStack { Spacer() Text(value.percentChange) Spacer() } } .width(90) } // end table .tableStyle(.inset(alternatesRowBackgrounds: true)) .padding([.leading, .trailing], 185) .padding([.top], 30) .padding([.bottom], 700) } }
0
0
736
Jan ’23
Swift - SQLite3 Wal Checkpoint -> Unsafe Pointer Parameter
I have a program that adds data from CSV files to core data. Once I have saved the managed object context to the persistent store I want to run a checkpoint to flush the contents of the WAL file into the SQLite file. I have added code to my class in an attempt to do so but am unable to determine on the line "sqlite3_wal_checkpoint( ...)" what to put in the unsafe pointer parameter (denoted with ??????? in the code). class UpdateCoreData: ObservableObject { @Published var isLoading: Bool = true var moc: NSManagedObjectContext init(moc: NSManagedObjectContext) { self.moc = moc Task { await IsDataStoreEmpty(moc: self.moc) let fund1 = CSVtoCoreData(fundName: "Fund1", fileName: "Fund1.csv") let fund2 = CSVtoCoreData(fundName: "Fund2", fileName: "Fund2.csv") let fund3 = CSVtoCoreData(fundName: "Fund3", fileName: "Fund3.csv") await fund1.CSVtoCoreDataG(moc: self.moc) await fund2.CSVtoCoreDataG(moc: self.moc) await fund3.CSVtoCoreDataG(moc: self.moc) do { try moc.save() } catch { print("Error saving. \(error)") } --- start of added code let fileURL = URL(fileURLWithPath: "/Users/Chris/Downloads/Persistent Store 2/Funds.sqlite") var dataBase: OpaquePointer? sqlite3_open(fileURL.path, &dataBase ) if sqlite3_open(fileURL.path, &dataBase) != SQLITE_OK { print("Unable to open sqlite file") } else { print("Succesfully opened sqlite file") } sqlite3_wal_checkpoint(dataBase, ??????? ) sqlite3_close(dataBase) --- end of added code DispatchQueue.main.async { self.isLoading = false } } } // end init }
0
0
577
May ’23