简体   繁体   English

SwiftUI 菜单按钮最初显示为禁用

[英]SwiftUI Menu Button Displayed as Disabled Initially

On macOS, SwiftUI Menu buttons appear initially disabled.在 macOS 上,SwiftUI 菜单按钮最初显示为禁用。 Once you click on them, they activate as normal and display properly.单击它们后,它们会正常激活并正确显示。 This code replicates the problem:这段代码复制了这个问题:

Menu {
    Button("First") { }
    Button("Second") { }
} label: {
    Image(systemName: "gearshape.fill")
}
.padding()

Which initially will look like this:最初看起来像这样:

在此处输入图像描述

Then after clicking on the button:然后点击按钮后:

在此处输入图像描述

  • Preview shows correctly, but a running app behaves as above预览显示正确,但正在运行的应用程序的行为如上
  • The contents of the menu don't seem to affect the result菜单的内容似乎不影响结果
  • Tried explicitly mucking with disabled state using .disabled(false) ;尝试使用禁用 state 显式处理.disabled(false) no joy since it isn't really disabled没有乐趣,因为它并没有真正被禁用

I could just set the foreground color of the image, but I was hoping to figure out the real problem.我可以设置图像的前景色,但我希望找出真正的问题。 Am I missing something?我错过了什么吗?

EDIT: Setting the foreground doesn't work either.编辑:设置前景也不起作用。 Still visibly disabled.仍然明显禁用。

This bug (filed as FB8976414) remains as of macOS 11.3.此错误(归档为 FB8976414)在 macOS 11.3 中仍然存在。 I got some help from @kontiki on a workaround, though.不过,我从@kontiki 那里得到了一些解决方法的帮助。 If you include a Button along with the Menu , it displays properly enabled.如果您在Menu中包含一个Button ,它会显示正确启用。 Don't ask me why.不要问我为什么。 You can make the Button zero-sized so it doesn't affect your layout and just remove it when (if?) the bug gets fixed.您可以使Button的大小为零,这样它就不会影响您的布局,并且在(如果?)错误得到修复时将其删除。

-- Update -- - 更新 -

As of macOS 11.4, this technique no longer works.从 macOS 11.4 开始,此技术不再有效。 The Menu always displays as disabled until it's clicked. Menu始终显示为禁用,直到它被单击。 I guess if you look on the bright side, at least it's more consistent!我想如果你看好的一面,至少它更一致!

-- -- -- --

HStack(spacing: 0) {
    Menu {
        Button("First") { }
        Button("Second") { }
    } label: {
        Image(systemName: "gearshape.fill")
    }

    Button("", action: {}).scaleEffect(x: 0, y: 0)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM