我创建一个UIButton
并将其放置在放置在UIImageView
上的UIScrollView
之上。我将按钮添加到图像中,如下所示:
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
,但事实是触摸事件也已注册。
最佳答案
您需要在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];
关于ios - iOS5上奇怪的UIButton行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16857976/