【问题标题】:UIButton does not work when it in UIScrollViewUIButton 在 UIScrollView 中不起作用
【发布时间】:2013-05-15 00:26:07
【问题描述】:

我的观点结构:

UITableView
  UITableViewCell
    UIScrollView
      CustomView
        UIButton

问题是当我触摸 UIButton 时它不起作用。 我用代码创建它:

btn = [[UIButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tileTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
btn.layer.zPosition = 1;
[self addSubview:btn];

我也添加了这个:

scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;

但我的按钮无论如何都不起作用。我无法为 UITableView 取消ContentTouches 或设置延迟,因为如果我设置它,UITableView 将停止垂直滚动。

感谢您的帮助!

【问题讨论】:

  • 你实现tileTouchUpInside方法了吗?
  • CustomView 的 userInteractionEnabled YES 吗?
  • @Capt.Hook - 谢谢,我试过了,没有帮助。另外,想要添加 - 当我只是将 CustomView 添加到 ViewController 时,没有我们的滚动,它工作得很好。如果放置在滚动视图中,它将停止工作。
  • @Capt.Hook 谢谢,你的链接对我有帮助。

标签: ios uiscrollview uibutton


【解决方案1】:

创建 UIScrollview 的子类

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return NO;
}

如果您没有收到该消息,请设置:

scrollView.delaysContentTouches = NO;

问题在于touchesShouldCancelInContentView 默认的行为方式,仅当子视图是 UIControl 时它才返回 NO,但您的按钮隐藏在 CustomView 中,而这不是。

【讨论】:

  • 您也可以在 IB 中关闭“延迟内容触摸”选项。这对我有用。
  • 滚动视图停止滚动
  • 还值得仔细检查 UIScrollView 不在小于预期的父视图中,使用 parentView.clipsToBounds = true;检查
【解决方案2】:

遇到了同样的情况。 UIScrollView 在单独的类中,并通过自定义视图添加按钮。 'NSNotificationCenter' 帮助我解决了这个问题。 示例代码:

-(void)tileTouchUpInside:(id)sender
{    
    NSDictionary *dict=[NSDictionary dictionaryWithObject:@"index" forKey:@"index"];

    [[NSNotificationCenter defaultCenter]postNotificationName:@"Action" object:nil userInfo:dict];
}

在包含Scroll View的类中:

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doAction:) name:@"Action" object:nil];
}
-(void)doAction:(NSNotification *) notification
{
    NSString* value =[notification.userInfo valueForKey:@"index"];
    // .......
}

为滚动视图、自定义视图和按钮启用 userInteraction。

【讨论】:

    【解决方案3】:

    我猜你需要设置按钮框架。默认情况下,如果您使用btn = [[UIButton alloc] init]; 方法来初始化按钮,它将是UIButtonTypeCustom。还要为其设置背景颜色以用于调试目的,以记下按钮实际放置的位置。 现在,对于您的情况,您需要为该按钮设置框架,例如 btn.frame = CGRectMake(0,0,100,100);

    对于内部 UIScrollView 实现这个委托方法。

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
      {
           return ![view isKindOfClass:[UIButton class]];
      }
    

    这可能有效。

    【讨论】:

    • 谢谢,我设置了按钮框架,只是没有在代码中说明这一点。
    【解决方案4】:

    没有一个答案对我有用,因为我的问题是按钮在布局时不可见或其他什么...

    修复:

    1) 将按钮完全移出scrollView(成为VC主view的子视图)。

    2) 从contentV 中选择您希望按钮定位的button 和元素(在我的情况下:在conventV 中的最后一个元素之后)

    3) 添加必要的约束(在我的例子中:底部对齐)

    4) 为button 添加其余约束。

    5) 享受。

    【讨论】:

    • 非常感谢!我的解决方案类似,我只需要相应地添加自动布局约束。尝试了很多方法,使用点击手势,将视图带到前面。没有任何效果。
    【解决方案5】:

    您的 CustomView 的框架可能会干扰您的 UIButton。它们重叠吗?将您的按钮添加到 CustomView 只是为了通过[CustomView addSubview:UIButton]; 进行测试(当然使用您的值)。如果它有效,那么您的问题很可能是重叠的。

    【讨论】:

    • 感谢您的想法。但是当我只是将我的 CustomView 添加到 ViewController 时,没有我们的滚动,它工作得很好。如果放置在滚动视图中,它将停止工作。
    • 如果您只想为表格视图添加一个按钮,请查看上一篇文章...stackoverflow.com/questions/10229597/…
    【解决方案6】:

    将 CustomView 中的所有子视图添加到 ScrollView。并隐藏CustomView。确保 CustomView(ContainerView of scrollView) 也应该是 scrollView 的子视图。

    【讨论】:

      【解决方案7】:

      Capt.Hook 的链接帮助了我。我使用 UIGestureRecognizer 而不是 UIButtons。

      Allow UIScrollView and its subviews to both respond to a touch 如果您有类似的问题,请使用它。

      【讨论】:

        【解决方案8】:

        我有相同的问题和相同的视图层次结构,使用最新的 sdk,只需使用它:

        将同一 UITableViewCell 中的 UIButton 的 delaysContentTouches 设置为 NO。

        self.tableView.delaysContentTouches = NO
        

        【讨论】:

          猜你喜欢
          • 2012-11-08
          • 2015-10-07
          • 2015-03-05
          • 1970-01-01
          • 2017-03-29
          • 1970-01-01
          • 2014-04-07
          • 2016-09-25
          相关资源
          最近更新 更多