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")
}
})
}
}
}
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.
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.
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.
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.
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:
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With