Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

macos - SwiftUI on Mac - How do I designate a button as being the primary?

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?

A Cancel button in the normal grey look, and a Save button in the system accent color. The text "Like this" is inserted with an arrow pointing to the Save button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...