我正在开发Xamarin Studio(MonoTouch),但概念是一样的。
我有一个UIViewController,在UIViewController中,我添加了几个子视图,每个子视图都需要响应tap事件。我将UITapGestureRecognizer附加到我的子视图中,但它们不会触发。查看“AddExploreButton()”和“AddFollowingButton()”。
如果我将手势添加到包含的视图(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);
}
}
答案 0 :(得分:2)
我发现了问题。我正在使用UIView.SizeToFit()来生成宽度和高度。
根据这个:Having trouble getting UIView sizeToFit to do anything meaningful但是,UIView.SizeToFit()没有做任何事情。
如果没有适当的帧大小,尽管可见,但子视图无法捕获触摸事件。