所以我有一个 UIView,我称之为 ctrView,这个 ctrView 被添加到我的 UIViewController 的 View 中。它基本上只是一个较小的矩形框架。我在这个容器 View 的右上角添加了一个 UIButton,但是似乎当单击 ctrView 之外的 UIButton 区域时不会触发与按钮相关的操作。只有容器 View 内的 UIButton 区域会触发操作。我该如何解决这个奇怪的问题?
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[closeButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
[closeButton setBackgroundColor:[UIColor clearColor]];
[closeButton addTarget:self action:@selector(closeZoomedImageView) forControlEvents:UIControlEventTouchUpInside];
[closeButton setCenter:CGPointMake(ctrView.frameX + ctrView.frameWidth - 30, ctrView.frameY - 15)];
[ctrView addSubview:closeButton];
[closeButton release];
最佳答案
这是正常行为,父级边界之外的 View 不会收到触摸 Action
为了解决这个问题,重新排列如下 View
主视图将同时具有内部 View 和关闭按钮作为其 subview
主视图将足够大以容纳按钮
主视图将具有清晰的颜色背景,因此不会影响布局
因此,您将在内部 View 中添加当前拥有的所有 View ,
内部 View 和按钮 View 都将添加到主视图中
然后将主视图呈现给 View Controller
关于iphone - subview 外的 UIButton 可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863913/