

SwiftUI Bug – PlainButtonStyle Button 触发次数异常
source link: https://www.hawu.me/coding/2591
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

SwiftUI Bug – PlainButtonStyle Button 触发次数异常
Base on macOS 10.15, Xcode 11.7.
1、当程序运行在非活跃状态时,且 NSWindow 为默认样式(可关闭、缩放、有标题栏等)。
此时如果点击 NSWindow 中的按钮,一般情况下会将该 NSWindow 变成活跃状态,同时触发按钮的 action 一次。但对于 PlainButtonStyle 样式的按钮,却无法触发其事件函数!
2、当程序运行在非活跃状态时,且 NSWindow 为空样式(styleMask = []),且允许拖动(isMovableByWindowBackground = true)。
此时如果点击 NSWindow 中的按钮,一般情况下会将该 NSWindow 变成活跃状态,同时触发按钮的 action 一次。但对于 PlainButtonStyle 样式的按钮,却会触发其事件函数两次!
import SwiftUI struct ContentView: View { var body: some View { VStack { Button("Normal") { NSLog("normal Button clicked") } Button(action: { NSLog("LinkButton clicked") }){ Text("LinkButton") }.buttonStyle(LinkButtonStyle()) Button(action: { NSLog("BorderedButton clicked") }){ Text("BorderedButton") }.buttonStyle(BorderedButtonStyle()) Button(action: { NSLog("BorderlessButton clicked") }){ Text("BorderlessButton") }.buttonStyle(BorderlessButtonStyle()) Button(action: { NSLog("PlainButton clicked") }){ Text("PlainButton") }.buttonStyle(PlainButtonStyle()) }.frame(width: 400, height: 300) } }
import Cocoa import SwiftUI @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { var window: NSWindow! func applicationDidFinishLaunching(_ aNotification: Notification) { // Create the SwiftUI view that provides the window contents. let contentView = ContentView() // 👇 When App runs in backend, PlainButtonStyle Button could be triggered window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false) // 👇 When App runs in backend, PlainButtonStyle Button trigger twice // window = NSWindow( // contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), // styleMask: [], // backing: .buffered, defer: false) // window.isMovableByWindowBackground = true window.center() window.setFrameAutosaveName("Main Window") window.contentView = NSHostingView(rootView: contentView) window.makeKeyAndOrderFront(nil) } func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } }
我把项目中的所有 PlainButtonStyle 按钮全部改成 BorderlessButtonStyle 了。
Recommend
-
34
In SwiftUI a button is a control that performs a action when triggered. In this tutorial a button can be clicked to increment a value.SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be
-
9
Posted 1 month ago2021-01-27T17:30:00+01:00 by Peter Steinberger Updated 1 month ago2021-02-03T16:12:59+01:00
-
7
iOS 8 Jquery Submit Button Focus Bug advertisements Recently we started to receive some complaints that the button on a page stopped to work u...
-
21
The many faces of button in SwiftUI 30 Jun 2021Button is one of the crucial components of any app. We use buttons to provide actions in the user interface of the app. SwiftUI 3 released a bunch of new view modifiers that allow us t...
-
12
How to make a custom button style supports leading dot syntax in SwiftUI Table of ContentsYou can easily support sarunw.com by checking out this sponsor.
-
4
股价连续下跌触发异常波动情形 ST宏达作出相关说明 5月16日,ST宏达再发股票交易异常波动公告,称公司股票交易价格连续3个交易日(2022年5月11日、2022年5月12日和20...
-
6
iOS 16被爆存在Apple ID登录异常Bug-51CTO.COM
-
11
SwiftUI 学习记录 - 切换 TabView 的 tab 时触发震动反馈 2023-02-06...
-
4
chuka231's blog [Bug?] Submit but...
-
6
SwiftUI的onAppear在NavigationView中不触发的问题 | 张洪Heo 博客快捷键 shift 关闭快捷键功能 shift 打开...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK