SwiftUI 菜单按钮最初显示为禁用 [英] SwiftUI Menu Button Displayed as Disabled Initially

查看:36
本文介绍了SwiftUI 菜单按钮最初显示为禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 macOS 上,SwiftUI 菜单按钮最初显示为禁用.单击它们后,它们会正常激活并正确显示.此代码复制了问题:

On macOS, SwiftUI Menu buttons appear initially disabled. 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()

最初看起来像这样:

然后点击按钮后:

  • 预览显示正确,但正在运行的应用表现如上
  • 菜单内容似乎不影响结果
  • 尝试使用 .disabled(false) 在禁用状态下进行显式处理;没有快乐,因为它并没有真正被禁用
  • 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); 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?

设置前景也不起作用.仍然明显被禁用.

Setting the foreground doesn't work either. Still visibly disabled.

推荐答案

这个错误(归档为 FB8976414)从 macOS 11.3 开始仍然存在.不过,我从 @kontiki 那里得到了一些解决方法的帮助.如果将 ButtonMenu 一起包含,它会正确显示启用.不要问我为什么.您可以将 Button 设为零大小,这样它就不会影响您的布局,并在(如果?)错误得到修复时将其删除.

This bug (filed as FB8976414) remains as of macOS 11.3. I got some help from @kontiki on a workaround, though. If you include a Button along with the Menu, it displays properly enabled. 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.

-- 更新 --

从 macOS 11.4 开始,此技术不再有效.Menu 始终显示为禁用状态,直到被点击.我想如果你从好的方面看,至少它更一致!

As of macOS 11.4, this technique no longer works. The Menu always displays as disabled until it's clicked. 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)
}

这篇关于SwiftUI 菜单按钮最初显示为禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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