【问题标题】:Strange UIButton behaviour on iOS5iOS5 上奇怪的 UIButton 行为
【发布时间】:2013-05-31 13:04:22
【问题描述】:

我创建了一个UIButton 并将它放在我放在UIScrollView 上的UIImageView 之上。我将按钮添加到图像中,如下所示:

backgroundImage.frame = CGRectMake(0, 0, backgroundImage.frame.size.width, mainTextView.frame.origin.y + mainTextView.frame.size.height + bottomMargin + attachBlockHeight);
        [self insertSubview:backgroundImage atIndex:0];

        UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, imgGreyLine.frame.origin.y + imgGreyLine.frame.size.height, backgroundImage.frame.size.width, attachBlockHeight)]; 
        [tmpButton addTarget:self action:@selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
        //tmpButton.backgroundColor = [UIColor redColor];
        [backgroundImage addSubview:tmpButton];

并且该按钮在 iOS6 及更高版本上运行良好 - 它就在那里,它接收触摸事件并执行它必须做的事情。

但是,在 iOS5 上,我遇到了一个非常奇怪的行为 - 按钮的 frame 保持完全相同,并且按钮在 view 上明显存在,但它接收当我的手指向左或向右滑动触摸事件。而且,按钮顶部附近没有其他UIViews

这可能是什么原因?我很困惑在这里找到错误。

编辑:事实证明滑动UIButton 也适用于iOS6,但问题是触摸事件也被注册了。

【问题讨论】:

  • 此场景中涉及的任何手势识别器?
  • @Stavash 不,没有一个
  • 视图层次结构中发生了什么?我看到你在所有其他视图下插入 UIImageView
  • 我认为问题在于 AutoResizeSubview 复选框。请取消选中所有 autoresizesubview 复选框。
  • @Stavash 我将自定义 UIView 添加到 UIScrollView。这个视图包含 index:0 处的图像、一个标签和一个 UIButton。这就是它的工作原理

标签: ios cocoa-touch uiview uibutton


【解决方案1】:

您需要在 UIImageView 父级上启用用户交互

例如:

UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backgroundImage setBackgroundColor:[UIColor yellowColor]];
[backgroundImage setUserInteractionEnabled:YES];
[self.view addSubview:backgroundImage];

UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,50,50)];
[tmpButton addTarget:self action:@selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
tmpButton.backgroundColor = [UIColor redColor];
[backgroundImage addSubview:tmpButton];

【讨论】:

  • 没关系,我仍然看不到它在一个系统中如何工作并在另一个系统中改变行为。这里不可能有一些 userInteraction BOOL。
【解决方案2】:

您不能将 UIbutton 添加为 UIImageview 的子视图。将其添加到滚动视图或 UIimageview 的超级视图中

【讨论】:

  • 看在上帝的份上,为什么我不能呢?即使我将它添加到 UIImageView 的超级视图中,也没有任何变化,正如预期的那样。
  • 你能贴出你的按钮@SergiusGee的目标方法吗
  • 这不会有任何区别,但是给你 - -(void)btnAttachClicked{ NSLog(@"CLICKED!!!"); }
【解决方案3】:

我认为做你想做的事情的正确方法是让 UIView 与 imageview 和按钮作为兄弟姐妹/孩子。确保禁用图像视图上的用户交互,你应该很好。

【讨论】:

  • 我确实禁用了用户交互,甚至在将 UIButton 添加到 UIImageView 的超级视图之后 - 承载所有这些的 UIView 并且添加到 UIScrollView - 没有任何效果!
猜你喜欢
  • 1970-01-01
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多