Difference between Buttons and onTapGesture
Asked Answered
B

4

11

I’m quite new to swiftui and I was curious on what’s the difference between using a button displaying an image or an image with onTapGesture modifier that make an action just like the action on buttons.

Beachlamar answered 5/5, 2020 at 1:27 Comment(7)
Buttons have several states you can use. You may want to look at collection views if you are displaying images that will be tapped.Rockefeller
So basically they’re the same but buttons have more functionality? I’m trying to create a tab view and, since I don’t know which one should I use and in which case, I’m using images as icons and on tap they change view. Is it fine or is better to use buttons?Beachlamar
Hey @xmetal, I would recommend using a UITabBarController and customizing it as needed. It is specifically built for your use-case: developer.apple.com/documentation/uikit/uitabbarcontrollerRockefeller
I tried to use it but it’s way to hard to customize it as I want...I couldn’t even modify the height of the tabBar or scale tab items in a specific way and I searched a lot about that. I have full control of it if I make it myselfBeachlamar
I would highly recommend customizing the tab bar any way you want. Here is how to customize height: https://mcmap.net/q/265432/-change-uitabbar-height and here is a tutorial on customizing the tab bar: codementor.io/@nguyentruongky/…Rockefeller
Either way, best of luck.Rockefeller
Thank you very much for your advices! I actually manage to make a tabBar using a ForEach loop that display images based on an array of string and a variable that stores the tab height. It works perfectly in multiple devices and just in few lines.Beachlamar
S
4

In the use case you've described you can use either but for reference, Apple provides the following guidance in their documentation for onTapGesture:

If you are creating a control that’s functionally equivalent to a Button, use ButtonStyle to create a customized button instead.

onTapGesture adds an action to perform when the view recognizes a tap gesture and can be tailored to respond to any number of taps (with 1 being the default).

Buttons are a control that performs an action when triggered and have the benefit of automatically adapting their visual style to match the expected style within different containers and contexts.

In iOS and watchOS, the user triggers a standard button by tapping on it. In macOS, the user triggers a standard button by clicking on it. In tvOS, the user triggers a standard button by pressing “select” on an external remote, like the Siri Remote, while focusing on the button.

For more information please refer to Apples documentation here: https://developer.apple.com/documentation/swiftui/text/ontapgesture(count:perform:) https://developer.apple.com/documentation/swiftui/button

Satiate answered 3/2, 2021 at 3:57 Comment(0)
I
1

The biggest difference between using a true Button and adding onTapGesture to an Image or shape is whether you want to do something detailed with different styles of presses. The Button class absorbs taps, and even re-issues them at the end of long presses... so if you want to combine a tap for one thing or a long press for another, a Button makes this very, very difficult to do, while it's trivial with an Image.

On the other hand, if you want it to act like a "normal" button (flash a bit when tapped, have a hit area slightly bigger than its content, etc), well, that's automatically built-in to Button, and a bit difficult with just adding tapGestures to other types of objects.

Interglacial answered 24/6, 2023 at 22:19 Comment(0)
J
0

I would recommend using Button when you want the feature of a button. I noticed that images cannot be used as buttons with Voice Over on. When I changed those to use Button instead, it fixes the problem.

Joh answered 9/12, 2022 at 6:56 Comment(0)
I
0

Choose Button when you want a standard button, and use onTapGesture when you need to add tap gesture behavior to a custom view.

Illdisposed answered 23/11, 2023 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.