1

I have been using TapGestureRecognizer for a while now, I had this issue from the beginning (the first time when I tested the app on an IOS device, for some reason I am having issues running a simulator, so I have been using a physical IOS device (iPhone 12 Pro Max)) and it seems that my TapGestureRecognizer is not being triggered on the IOS device. On UWP (which I mainly test on as it is quickest) I am able to switch between tabs, etc. on my app, (I am using TapGestureRecognizer for all buttons).

Here is one of the more simple examples where I am able to use the buttons in UWP but not on IOS.

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="fotbal.Oblibene" xmlns:viewModel="clr-namespace:fotbal;assembly=fotbal" xmlns:fotbal="clr-namespace:fotbal">
    <Grid x:Name="Menu_Grid" />
</ContentPage>

CS:

Menu_Grid.ColumnSpacing = 30;

Menu_Grid.Margin = new Thickness(15, 0, 0, 0);

for (int i = 0; i < 5; i++) Menu_Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
Menu_Grid.RowDefinitions.Add(new RowDefinition());

var page_switcher_tap_recognizer = new TapGestureRecognizer();
page_switcher_tap_recognizer.Tapped += (sender, e) =>
{
    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
};

int j = 0;
            foreach (KeyValuePair<string, string> kvp in oblibene_categories)
            {
                Grid grid = new Grid
                {
                    RowDefinitions = { new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, new RowDefinition { Height = new GridLength(3) } },
                    ColumnDefinitions = { new ColumnDefinition() }
                };

                Label label = new Label { TextColor = Color.FromHex("#888888"), Text = kvp.Key, FontAttributes = FontAttributes.Bold, FontSize = 15, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center };

                BoxView boxView = new BoxView { Color = Color.White };
                if (kvp.Value == selected_category)
                {
                    label.TextColor = Color.FromHex("#439564");
                    boxView.Color = Color.FromHex("#439564");
                }

                var label_page_switcher_tap_recognizer = new TapGestureRecognizer();
                label_page_switcher_tap_recognizer.Tapped += (sender, e) =>
                {
                    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
                };

                label.GestureRecognizers.Add(label_page_switcher_tap_recognizer);

                var boxview_page_switcher_tap_recognizer = new TapGestureRecognizer();
                boxview_page_switcher_tap_recognizer.Tapped += (sender, e) =>
                {
                    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
                };
                boxView.GestureRecognizers.Add(boxview_page_switcher_tap_recognizer);

                grid.Children.Add(label);

                grid.Children.Add(new NeatFrame { Content = boxView, CornerRadius = 2 }, 0, 1);

                var grid_page_switcher_tap_recognizer = new TapGestureRecognizer();
                grid_page_switcher_tap_recognizer.Tapped += (sender, e) =>
                {
                    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
                };
                grid.GestureRecognizers.Add(grid_page_switcher_tap_recognizer);

                Menu_Grid.Children.Add(grid, j, 0);
                j++;
            }
MartinNajemi
  • 514
  • 3
  • 18
  • 1
    Does this answer your question? [Can you attach a UIGestureRecognizer to multiple views?](https://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views). On iOS, you have to create a **new recognizer** for each view. You can’t attach page_switcher_tap_recognizer to multiple views. – ToolmakerSteve Jan 12 '22 at 22:17
  • @ToolmakerSteve Hi, I have tried this however am experiencing the same issue. – MartinNajemi Jan 16 '22 at 16:53
  • Unless you show the exact code you tried, there is no way for anyone to help you figure out why it did not work. – ToolmakerSteve Jan 16 '22 at 19:33
  • @ToolmakerSteve Apologies, I have updated the question – MartinNajemi Jan 19 '22 at 19:29
  • Code looks good. Do you call that code from constructor? As a test, replace the entire code in loop with just boxview (`Menu_Grid.Children.Add(boxview, j, 0);`) and its tap recognizer. (no grid per element in the loop) See if that responds on iOS. – ToolmakerSteve Jan 19 '22 at 22:05
  • 1) OR maybe the label instead of boxview - easier to see to click on. If there is a Xamarin bug on iOS, I suspect tap on `Grid` somehow being an issue. SO want simplest possible cell. No Grid. No NeatFrame. Goal is to isolate the cause. ALSO, for test, add children from method called by Constructor, not later on in OnAppearing or other later method. 2) Have you set breakpoints inside the `Tapped` handlers, to see whether any of them are being reached? (To make sure it really is tap not working, rather than a problem with the code that is called.) – ToolmakerSteve Jan 19 '22 at 22:25

1 Answers1

0

As ToolmakerSteve mentioned , a gesture only works on a single view, you can't set same the gesture for multiple views.

So create the different gestures and do the same thing and then add on the views one by one.

var page_switcher_tap_recognizer = new TapGestureRecognizer();
page_switcher_tap_recognizer.Tapped += (sender, e) =>
{
    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
};

var page_switcher_tap_recognizer2 = new TapGestureRecognizer();
page_switcher_tap_recognizer2.Tapped += (sender, e) =>
{
    tab_switcher(sender, Menu_Grid, Oblibene_ScrollView);
};


label.GestureRecognizers.Add(page_switcher_tap_recognizer);
boxView.GestureRecognizers.Add(page_switcher_tap_recognizer2);
ColeX
  • 14,062
  • 5
  • 43
  • 240