Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hard to tap button in SwiftUI navigation bar items

Tags:

ios

swift

swiftui

I have a SwiftUI NavigationView with a Button as leading navigation bar item. It seems the button action is fired only when user taps inside that little 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.

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")
                        }
                    })
            }
    }
}
like image 916
Kristoffer Jälén Avatar asked Mar 03 '20 19:03

Kristoffer Jälén


People also ask

How do I manage navigation in SwiftUI?

SwiftUI's NavigationLink has a second initializer that has an isActive parameter, allowing us to read or write whether the navigation link is currently active. In practical terms, this means we can programmatically trigger the activation of a navigation link by setting whatever state it's watching to true.

How to customize the navigation bar in SwiftUI?

Customizing the navigation bar in SwiftUI by inserting bar items is super easy using the .toolbar modifier. Here’s how to do it! Step 1: Make sure your whole view is wrapped into a NavigationView.

Is it possible to use a toolbar in SwiftUI?

There is a downside with toolbars in SwiftUI, and that is that it cannot be used in iOS versions less than 14. If you are planning to support both iOS 13 and 14+, then I’m afraid that you are a bit out of luck, but not entirely. You may use the view modifiers related to the navigation bar instead of a toolbar at the top.

How to dismiss a sheetview when tapped in SwiftUI?

Sheet view presented modally in SwiftUI In order to dismiss the SheetView, user needs to pull down on it (swipe down). However, what if we wanted to add a “Done” button to the SheetView which will dismiss it when tapped? Let’s explore how to achieve it.

How to place a toolbar item on the navigation bar?

Starting with the first one, let’s place a new toolbar item to the leading edge of the navigation bar. On tap, it will be presenting a modal sheet: Once again, we are initializing a ToolbarItem view where we provide the desired placement as argument. Its content is a Button that:


1 Answers

(One of) the following modifiers could help:

.imageScale(.large)

A image from SFSymbols has three sizes:

  • .small for when using inline with text
  • .medium for use as an icon
  • .large for use as a button in a nav bar or bottom bar

.padding()

Adds padding around the image. The padding should also be tappable.

like image 66
cbjeukendrup Avatar answered Nov 28 '22 11:11

cbjeukendrup