Chris
1
Hello,
I have a Swift view that incorporates a Scroll View. It displays the correct data and allows me to move through the associated items with the mouse. What it does not do is allow me to use the keyboard buttons such as arrow up, arrow down, page up and page down. I am at a loss on how to incorporate this functionality into my view. Any guidance would be appreciated.
Below is my code.
Regards,
Chris
import SwiftUI
struct SecondView: View {
var columns = [
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1),
GridItem(.fixed(100), spacing: 0.1)
]
let principalData: convertCSVtoArray
var body: some View {
let numArrays = principalData.csvData.count
let numElementsPerArray = principalData.csvData[0].count
VStack{
Text("")
Text("Historical Data")
.font(.title)
.padding(5)
Divider()
LazyVGrid(columns: columns, alignment: .center, spacing: 0)
{
ForEach(0..<1) {row in
ForEach(0..<numElementsPerArray) {col in
returnRectangle(theRow: row, theCol: col, principalData: principalData)
}
}
}.padding(.bottom, -7.5)
ScrollView{
LazyVGrid(columns: columns, alignment: .center, spacing: 0
)
{
ForEach(1..<numArrays) {row in
ForEach(0..<numElementsPerArray) {col in
returnRectangle(theRow: row, theCol: col, principalData: principalData)
}
}
}
ForEach(0..<30){index in
Text("")
}
}// end scrollview
}
}
}
tem
(GalaxySwift)
2
I haven't been able to find a simple answer to this. As far as I can tell you'd need to use a combination of the focusedValue modifier (or focused in macOS 12+), the keyboardShortcut modifier (on an invisible button perhaps), GeometryReader, and ScrollViewReader, to make this happen.
Something like:
PgDn key is pressed → keyboardShortcut runs a Button action → which checks if the ScrollView is focused → if so, take the current scroll offset from GeometryReader and add the view height to it → then call scrollTo on the proxy provided by ScrollViewReader
I feel like there must be a better way, but I'm not seeing it.
If you want to do it this way, here are some tutorials to help you: