在 SwiftUI 中隐藏一个按钮以在 MacOS 上使用其键盘快捷方式
Hide a button in SwiftUI to use its keyboardShortcut on MacOS
我的视图中有带键盘快捷方式的按钮以接收键盘快捷方式,但我不希望按钮可见。
我怎样才能最好地隐藏它们?
我试过这样,但如果我将 .buttonStyle(PlainButtonStyle())
添加到组中,带箭头的按钮将不再起作用(默认和转义)。
import SwiftUI
struct HiddenCommandbutton: View {
@State var showMiniDialog = false
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button("show"){showMiniDialog = true}
.sheet(isPresented: $showMiniDialog){
MiniDialog()
}
}
}
}
struct MiniDialog: View {
@Environment(\.dismiss) var dismiss
@State var sel: Int?
var options = ["Opt1", "Opt2", "Opt3", "Opt4"]
var rEdge:Int { options.count-1}
var body: some View {
ZStack{
Group{
Button(""){dismiss()}.keyboardShortcut(.cancelAction)
Button(""){dismiss()}.keyboardShortcut(.defaultAction)
Button(""){left()}.keyboardShortcut(.leftArrow)
Button(""){right()}.keyboardShortcut(.rightArrow)
}//.buttonStyle(PlainButtonStyle())
.padding(0)
HStack{
ForEach(options.indices) { index in
option(options[index],pos: index)
}
}
}
.padding(4)
}
func left(){
if sel == nil {
sel = rEdge
} else if sel! > 0 {
sel = sel! - 1
}
}
func right(){
if sel == nil {
sel = rEdge
} else if sel! < rEdge {
sel = sel! + 1
}
}
@ViewBuilder func option(_ title: String, pos: Int) -> some View {
if (sel != nil && sel! == pos) {
Text(title)
.padding(4)
.background(Color.red)
.cornerRadius(5.0)
} else {
Text(title)
.padding(4)
.cornerRadius(5.0)
}
}
}
试试这个
Group{
Button(""){dismiss()}.keyboardShortcut(.cancelAction)
Button(""){dismiss()}.keyboardShortcut(.defaultAction)
Button(""){left()}.keyboardShortcut(.leftArrow)
Button(""){right()}.keyboardShortcut(.rightArrow)
}
.opacity(0) // << here !!
我的视图中有带键盘快捷方式的按钮以接收键盘快捷方式,但我不希望按钮可见。
我怎样才能最好地隐藏它们?
我试过这样,但如果我将 .buttonStyle(PlainButtonStyle())
添加到组中,带箭头的按钮将不再起作用(默认和转义)。
import SwiftUI
struct HiddenCommandbutton: View {
@State var showMiniDialog = false
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button("show"){showMiniDialog = true}
.sheet(isPresented: $showMiniDialog){
MiniDialog()
}
}
}
}
struct MiniDialog: View {
@Environment(\.dismiss) var dismiss
@State var sel: Int?
var options = ["Opt1", "Opt2", "Opt3", "Opt4"]
var rEdge:Int { options.count-1}
var body: some View {
ZStack{
Group{
Button(""){dismiss()}.keyboardShortcut(.cancelAction)
Button(""){dismiss()}.keyboardShortcut(.defaultAction)
Button(""){left()}.keyboardShortcut(.leftArrow)
Button(""){right()}.keyboardShortcut(.rightArrow)
}//.buttonStyle(PlainButtonStyle())
.padding(0)
HStack{
ForEach(options.indices) { index in
option(options[index],pos: index)
}
}
}
.padding(4)
}
func left(){
if sel == nil {
sel = rEdge
} else if sel! > 0 {
sel = sel! - 1
}
}
func right(){
if sel == nil {
sel = rEdge
} else if sel! < rEdge {
sel = sel! + 1
}
}
@ViewBuilder func option(_ title: String, pos: Int) -> some View {
if (sel != nil && sel! == pos) {
Text(title)
.padding(4)
.background(Color.red)
.cornerRadius(5.0)
} else {
Text(title)
.padding(4)
.cornerRadius(5.0)
}
}
}
试试这个
Group{
Button(""){dismiss()}.keyboardShortcut(.cancelAction)
Button(""){dismiss()}.keyboardShortcut(.defaultAction)
Button(""){left()}.keyboardShortcut(.leftArrow)
Button(""){right()}.keyboardShortcut(.rightArrow)
}
.opacity(0) // << here !!