ios - iOS5上奇怪的UIButton行为

标签 ios cocoa-touch uiview uibutton

我创建一个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/

相关文章:

ios - 在iPad的计时器应用程序中模仿计时器

ios - 在 iOS 上更改方向后 Webview 随机不重新调整

iphone - 如何判断 UIView 是否可见并显示在屏幕上?

iphone - NSKeyedUnarchiver - 如何防止崩溃

ios - 如果在 block 中引用 self,为什么这不是一个保留循环?

iOS - 按需添加标签?

ios - Tint an Image(必须有更简单的方法)

ios - 使用 Firebase 过滤搜索?

ios - 是否可以执行溶解动画但控制进度,例如如何使用 Alpha?

iphone - UIView 自动调整资源大小