简体   繁体   English

难以点击 SwiftUI 导航栏项目中的按钮

[英]Hard to tap button in SwiftUI navigation bar items

I have a SwiftUI NavigationView with a Button as leading navigation bar item.我有一个带有Button的 SwiftUI NavigationView作为领先的导航栏项目。 It seems the button action is fired only when user taps inside that little Image .似乎只有当用户点击那个小Image时才会触发按钮动作。 Can I make the tappable area bigger, without affecting the height of the navigation bar?我可以在不影响导航栏高度的情况下将可点击区域放大吗?

I tried adding .frame to the Image , but that made the navigation bar too big.我尝试将.frame添加到Image ,但这使导航栏太大。

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Foo")
                .navigationBarTitle(Text("Title"), displayMode: .inline)
                .navigationBarItems(leading:
                    HStack {
                        Button(action: {
                            print("tapped")
                        }) {
                            Image(systemName: "info.circle")
                        }
                    })
            }
    }
}

(One of) the following modifiers could help: (其中一个)以下修饰符可以提供帮助:

.imageScale(.large)

A image from SFSymbols has three sizes:来自 SFSymbols 的图像具有三种尺寸:

  • .small for when using inline with text .small用于内嵌文本
  • .medium for use as an icon .medium用作图标
  • .large for use as a button in a nav bar or bottom bar .large用作导航栏或底部栏中的按钮

.padding()

Adds padding around the image.在图像周围添加填充。 The padding should also be tappable.填充也应该是可点击的。

Try this out试试这个

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Foo")
                .navigationBarTitle(Text("Title"), displayMode: .inline)
                .navigationBarItems(leading:
                    HStack {
                        Button(action: {
                            print("tapped")
                        }) {
                            Image(systemName: "info.circle").imageScale(.large) //Here is the change in image scale property
                        }
                    })
            }
    }
}

Happy Coding...快乐编码...

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

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