【发布时间】:2021-01-05 10:24:35
【问题描述】:
我需要构建这样的布局,并且按钮应接受键盘快捷键(AppKit):
在Let use a button all available width in SwiftUI on MacOS AppKit 我找到了一个解决方案,它创建了布局,但是这个 ExpandingButton 不接受键盘快捷键 - 可能是因为它是一个 HStack。
另一个想法,在上面的帖子中提出给标准按钮一个 .frame(maxWidth: .infinity) 修饰符对我不起作用。
struct TEST: View {
let columns = [
GridItem(.flexible()),
GridItem(.flexible()),
GridItem(.flexible())
]
let data = ["1 Text ...",
"2 longer Text ...",
"3 Text ...",
"4 Text/FA/Tra",
"5 Text ...",
"6 Text ...",
"7 Text ...",
"8 Text ...",
"9 Text ...",
]
var body: some View {
VStack (alignment: .leading ){
LazyVGrid(columns: columns) {
ForEach(data.indices, id: \.self) { index in
ExpandingButton(text:data[index],action: {print("pressed \(index)")})
.keyboardShortcut(KeyEquivalent(Character(UnicodeScalar(0x0030+index)!)) , modifiers: [.command])
}
}
}
.padding(.horizontal)
}
ExpandingButton(s: "Hogo"){print("hogo")}
ExpandingButton(s: "Hogo"){print("hogo")}
ExpandingButton(s: "Hogo"){print("hogo")}
}
}
struct ExpandingButton: View {
var s : String
var action: ()->Void
var body: some View {
HStack (alignment: .top){
Spacer()
Text(s)
.padding(4)
Spacer()
}
.background(Color.gray)
.cornerRadius(4)
.onTapGesture {action()}
}
}
【问题讨论】:
标签: swiftui keyboard-shortcuts appkit