【iOS】为什么在Category中创建的UIView的点击事件无效

我们工程中为了实现应用内的消息通知,所以新建了Category来响应推送通知,并提示用户。但是当消息框弹出时,虽然给UIView加上了UITapGestureRecognizer,同时也设置了setUserInteractionEnabled:YES,但无论怎么点击这个view都无法响应到Category中的点击事件。在这个UIView上加了一个button,点击也是无效的,这是为什么呢?

贴一下关键代码:

    UIView* notificationView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 44)];

    UIButton* redirectButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
    [redirectButton addTarget:self action:@selector(**showMessageView**) forControlEvents:UIControlEventTouchUpInside];
    redirectButton.backgroundColor = [UIColor clearColor];
    [redirectButton setTitle:@"跳转!" forState:UIControlStateNormal];
    [redirectButton setUserInteractionEnabled:YES];
    [notificationView addSubview:redirectButton];

    [self.view.window addSubview:notificationView];

无论怎么点击,都无法进入这个UIViewController+NotificationView的showMessageView方法。请大家帮忙捉下虫,谢谢!

阅读 5.9k
1 个回答

如果设置都没有问题。
通常会不会是因为View的覆盖所导致的?
或者View被不小心offset了?
通常是小错误。
先检查代码。
XCode6出了新的debug或者能够帮到你。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题