简体   繁体   English

带有菜单的 ToolbarItem 很难点击 - SwiftUI

[英]ToolbarItem with a Menu very hard to tap - SwiftUI

I try to create a (add/plus) button that shows a menu from which we can select the action we want to execute (it is similar to the Files, or Notes app on iPhone)我尝试创建一个(添加/加号)按钮,显示一个菜单,我们可以从中 select 我们想要执行的操作(它类似于 iPhone 上的文件或笔记应用程序)

.navigationTitle("Hello World Everyone")
.toolbar {
    ToolbarItem(placement: .navigationBarTrailing) {
        Menu(content: {
            Button(action: {print("option A")}, label: {Label("My option A", systemImage: "folder.badge.plus")})
            Button(action: {print("option B")}, label: {Label("My option B", systemImage: "doc.badge.plus")})
        }, label: {
            Image(systemName: "plus")
                .imageScale(.large)
                .background(Color.red)
        })
    }
}

But the issue is that it is extremely hard to tap on this "plus" button !但问题是点击这个“加号”按钮非常困难! On the other hand if instead of the Menu block I used a traditional Button block this issue disappears (but of course I loose the functionality that I want) (in Xcode 13b4 it is no-more the case)另一方面,如果我使用传统的按钮块而不是菜单块,这个问题就会消失(但当然我失去了我想要的功能) (在 Xcode 13b4 中不再是这种情况)

Do you have any ideas?你有什么想法?

EDIT 1:编辑 1:

Few people recommended me to add a padding to the label of Menu, which is a good idea but here is why it is not perfect:很少有人建议我在 Menu 的 label 中添加一个 padding,这是一个好主意,但这里是它不完美的原因:

The tappable area (represented in red) does not go until the trailing edge of the screen (see purple drawing). go 可点击区域(以红色表示)直到屏幕的后缘(见紫色图)。 So if by misfortune the user tapped on this side of the add button (purple zone), the click will not work: in other words it remains quite hard to tap on this button!因此,如果用户不幸点击了添加按钮的这一侧(紫色区域),则点击将不起作用:换句话说,点击此按钮仍然非常困难!
And another issue is that the plus icon is shifted on the left from his default position另一个问题是加号图标从他的默认 position 向左移动

If you use Button inside Toolbar SwiftUI will automatically converts it into ToolBarButton , which customisation is not possible on ToolBarButton .如果您在Toolbar中使用Button SwiftUI 将自动将其转换为ToolBarButton ,这在ToolBarButton上是不可能的。 Instead of passing a Button you can pass a Custom View .您可以传递 Custom View而不是传递Button

        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Menu(content: {
                    Label("My option A", systemImage: "folder.badge.plus")
                        .onTapGesture { 
                            print("My option A") 
                        }
                    Label("My option B", systemImage: "doc.badge.plus")
                        .onTapGesture { 
                            print("My option B") 
                        }
                }, label: {
                    Image(systemName: "plus")
                        .imageScale(.large)
                        .background(Color.red)
                })
            }
        }

I'm not sure you can expand the buttons inside the Toolbar beyond the "safe area".我不确定您是否可以将工具栏内的按钮扩展到“安全区域”之外。

I just wanted to let you know that the actual button in this case is the Menu and not the Image in the label, so you're not highlighting the full button.我只是想让您知道,在这种情况下,实际按钮是菜单,而不是 label 中的图像,因此您没有突出显示完整按钮。 See the full tapping area in green.查看绿色的完整攻丝区域。

Also using a bigger image, like "plus.circle" gives you a bigger area.还可以使用更大的图像,例如“plus.circle”,这样可以为您提供更大的区域。

Example image:示例图像:

示例图片

Example code:示例代码:
.navigationBarBackButtonHidden()
.navigationTitle("Hello World Everyone")
.toolbar {
    ToolbarItemGroup(placement: .navigationBarTrailing) {
        Menu(content: {
            Button(action: {print("option A")}, label: {Label("My option A", systemImage: "folder.badge.plus")})
            Button(action: {print("option B")}, label: {Label("My option B", systemImage: "doc.badge.plus")})
        }, label: {
            Image(systemName: "plus")
                .imageScale(.large)
                .background(Color.red)
        })
        .background(Color.green)
        Menu(content: {
            Button(action: {print("option A")}, label: {Label("My option A", systemImage: "folder.badge.plus")})
            Button(action: {print("option B")}, label: {Label("My option B", systemImage: "doc.badge.plus")})
        }, label: {
            Image(systemName: "plus.circle")
                .imageScale(.large)
                .background(Color.red)
        })
        .background(Color.green)
    }
}

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

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