ios - UIImageView 内的 UIButton 不响应点击

标签 ios objective-c cocoa-touch uiimageview uibutton

我有一个 ScrollView ,它有一个 ImageView , ImageView 作为 subview , ImageView 有一个 UIButton 作为它的 subview 之一。问题是,我无法点击按钮。我可以看到按钮,但无法点击它。

谁能告诉我我搞砸了什么?任何帮助是极大的赞赏!谢谢!

代码如下:

scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.jpg"]];
scrollView.delegate = self;
self.view = scrollView;

// add invisible buttons
[self addInvisibleButtons];
[scrollView addSubview:imageView];

addInvisibleButtons 具有以下代码:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:@"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
[self.imageView addSubview:button];

最佳答案

UIImageView 默认将 userInteractionEnabled 设置为 NO/false

您正在将按钮作为 subview 添加到 ImageView 中。您应该将其设置为 YES/true

关于ios - UIImageView 内的 UIButton 不响应点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164357/

相关文章:

ios - UITapGestureRecognizer 与 UISearchBar

objective-c - 程序在Ipad程序中收到信号sigabrt

ios - UISegmentedControl tintColor

ios - SwiftUI:将动画附加到过渡

iphone - 图层的过渡效果

ios - 如何使用 NSRange

java - 如何在 Sparkle updater(java app) 中等待用户操作?

objective-c - 如何在 View Controller 中从 NSDictionary 添加和检索数据

ios - 如何在 EKRecurrenceRule 中为星期几设置数组?

ios - 处理大量错误代码的设计模式