Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是什么导致iOS中的子视图无法捕获触摸?_Ios_Xamarin.ios_Xamarin - Fatal编程技术网

是什么导致iOS中的子视图无法捕获触摸?

是什么导致iOS中的子视图无法捕获触摸?,ios,xamarin.ios,xamarin,Ios,Xamarin.ios,Xamarin,我在Xamarin工作室(MonoTouch)开发,但概念是一样的 我有一个UIViewController,在该UIViewController中我添加了几个子视图,每个子视图都需要响应点击事件。我将UITapGestureRecognitor附加到我的子视图,但它们不会启动。请看“AddExploreButton()”和“AddFollowButton()” 如果我将手势添加到包含的视图(MyMenu.View),它会捕获点击手势,但我需要子视图来处理事件 是什么导致我的子视图不接收手势?(

我在Xamarin工作室(MonoTouch)开发,但概念是一样的

我有一个UIViewController,在该UIViewController中我添加了几个子视图,每个子视图都需要响应点击事件。我将UITapGestureRecognitor附加到我的子视图,但它们不会启动。请看“AddExploreButton()”和“AddFollowButton()”

如果我将手势添加到包含的视图(MyMenu.View),它会捕获点击手势,但我需要子视图来处理事件

是什么导致我的子视图不接收手势?(注意,我还将UserInteractionEnabled=true设置为几乎所有内容)

第二,如果这是不正确的编码模式,我如何连接子视图以接触事件

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);
    }
}

我发现了问题。我使用UIView.SizeToFit()生成宽度和高度

根据这一点:然而,UIView.SizeToFit()不做任何事情

如果没有适当的帧大小,尽管子视图可见,但无法捕获触摸事件。

View.UserInteractionEnabled=true;帮助我从第二个子视图捕捉触摸事件。谢谢