Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes Subviews in iOS to not capture touches?

I'm developing in Xamarin Studio (MonoTouch), but the concept is the same.

I have a UIViewController, and within that UIViewController I've added several subviews that each need to respond to tap events. I attach the UITapGestureRecognizer to my subviews, but they do not fire. Look at "AddExploreButton()" and "AddFollowingButton()".

If I add the gestures to the containing View (MyMenu.View), it does capture the tap gestures, but I need the subviews to handle the events.

What causes my subviews to NOT receive the gestures? (Note I've also set UserInteractionEnabled = true on practically everything).

Secondly, if this is the incorrect coding pattern, how else would I hook up subviews to touch events?

public class MyMenu : UIViewController
{
    private UIImageView _settingsButton;
    private TrendingMenuButton _trendingButton;
    private FollowingMenuButton _followingButton;
    private ExploreMenuButton _exploreButton;
    private NotificationsMenuButton _notificationsButton;

    public MyMenu() { }

    public override void ViewDidLoad() {
        base.ViewDidLoad();

        View.Hidden = true;
        View.UserInteractionEnabled = true;
        View.BackgroundColor = UIColor.FromPatternImage(UIImage.FromBundle("images/menu_bg.png"));
        View.Frame = new RectangleF(0, CityTitleBar.NAV_HEIGHT, 320, 349);

        AddSettingsButton();
        AddTrendingButton();
        AddFollowingButton();
        AddExploreButton();
        AddNotificationsButton();
    }

    private void AddSettingsButton() {
        _settingsButton = new UIImageView(UIImage.FromBundle("images/icon_cogwheel.png"));
        _settingsButton.Frame = new RectangleF(new PointF(269, 12), _settingsButton.Frame.Size);
        View.AddSubview(_settingsButton);
    }

    private void AddTrendingButton() {
        _trendingButton = new TrendingMenuButton(42, 33);
        View.AddSubview(_trendingButton);
    }

    private void AddFollowingButton() {
        _followingButton = new FollowingMenuButton(182, 33);
        _followingButton.AddGestureRecognizer(new UITapGestureRecognizer((g) => {
            MenuHelper.HideMenu();
            NavigationHelper.LoadScreen(new FavoritesScreen());
        }));
        View.AddSubview(_followingButton);
    }

    private void AddExploreButton() {
        _exploreButton = new ExploreMenuButton(42, 187);
        _exploreButton.AddGestureRecognizer(new UITapGestureRecognizer((g) => {
            MenuHelper.HideMenu();
            NavigationHelper.LoadScreen(new ExploreScreen());
        }));
        View.AddSubview(_exploreButton);
    }

    private void AddNotificationsButton() {
        _notificationsButton = new NotificationsMenuButton(182, 187);
        View.AddSubview(_notificationsButton);
    }
}
like image 556
Allison A Avatar asked Nov 25 '25 05:11

Allison A


1 Answers

I found the problem. I was using UIView.SizeToFit() to generate the width and height.

According to this: Having trouble getting UIView sizeToFit to do anything meaningful however, UIView.SizeToFit() does not do anything.

Without the appropriate Frame size, despite being visible, the subviews cannot capture touch events.

like image 83
Allison A Avatar answered Nov 27 '25 17:11

Allison A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!