Mac 上的 SwiftUI - 如何将按钮指定为主按钮? [英] SwiftUI on Mac - How do I designate a button as being the primary?

查看:22
本文介绍了Mac 上的 SwiftUI - 如何将按钮指定为主按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 AppKit 中,我会通过

解决方案

macOS 11.0/iOS 14:

从 Xcode 12 beta 开始,Button() 上公开了新方法,允许分配 keyEquivalent(通过枚举大小写或显式键和修饰符).

设置为默认值:

按钮( ... ).keyboardShortcut(.defaultAction)

设置为取消:

按钮( ... ).keyboardShortcut(.cancelAction)

In AppKit I would do this by assigning its key equivalent to be or making its cell the window's default. However, neither of these seems possible in SwiftUI, so how do I make a button the default window button?

解决方案

macOS 11.0 / iOS 14:

As of Xcode 12 beta, new methods are exposed on Button() allowing assignment of keyEquivalent (either by enum case or explicit key and modifiers).

Setting as default:

Button( ... )
    .keyboardShortcut(.defaultAction)

Setting as cancel:

Button( ... )
    .keyboardShortcut(.cancelAction)

这篇关于Mac 上的 SwiftUI - 如何将按钮指定为主按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆