按钮的 SwiftUI 可触摸区域 [英] SwiftUI Touchable Area of a Button

查看:50
本文介绍了按钮的 SwiftUI 可触摸区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加 NavigationView 中按钮的可触摸区域.即使该区域变大,它也不起作用.我的代码如下:

I am trying to increase the touchable area of a button inside a NavigationView. It does not work even though the area is made bigger. My code is below:

var body: some View {
NavigationView {
    List(taskStore.tasks) { tasks in
        Text(tasks.name)
    }
.navigationBarTitle("Tasks")
.navigationBarItems(
    trailing: Button(action: { 
    self.modalIsPresented = true 
}){
     Image(systemName: "plus")
    .frame(width: 200, height: 200)
    .contentShape(Rectangle())
    .background(Color.yellow)
})}

绿色区域可触摸,红色区域不可触摸.

The green area is touchable and the red area isn't touchable.

我在网上找到了一个有效的解决方案.但是,此解决方案仅适用于不在 NavigationView 中的按钮.因此,如果我将按钮放在某个视图"中,如下所示,它会按照解决方案工作:

I found a solution online that works. However this solution only works for a button that is NOT in the NavigationView. So if I put the button in "some view" like the following below, it works as per the solution:

var body: some View {
Button(action: {self.modalIsPresented = true} ) {
Text("Default padding")
.padding(50)
.background(Color.yellow)
}}}

但是当我像我的代码一样将按钮放在导航视图中时,黄色区域是不可触摸的.如何让整个黄色区域(红色框)像解决方案一样可触摸?

But when I put the button in a Navigation View like my code, the yellow area is not touchable. How can I get the whole yellow area (red box) to be touchable like the solution?

谢谢:D

解决方案示例:

推荐答案

如果你想在导航栏中有一个按钮,它只能在导航栏中点击,无论你尝试将图像的框架设置在什么位置,而 NavigationView 会确定该高度,无论子项(在本例中为按钮)可能想要什么.

If you want a button in the navigation bar, it is only going to be clickable inside the navigation bar, no matter what you try to set the image's frame at, and the NavigationView determines that height, no matter what the children- the button, in this case- may want.

过去,不支持更改 NavigationBar 的高度:请参阅 此处的评论

Historically, changing the height of the NavigationBar has not been supported: see the comments here

现在你可以用 ZStacks 做一些时髦的事情——在导航视图的顶部放置一个按钮,也许——但是你不能在导航栏中放置任何大于设置高度的东西.

Now you could do something funky with ZStacks- put a button on top of the navigation view, perhaps- but you're not going to be able to put anything larger than the set height inside the navigation bar.

这篇关于按钮的 SwiftUI 可触摸区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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