我无法触摸的 UIButton
我在 pickerView 上方创建了一个按钮来关闭它。 我想我做到了 这里是启动 pickerView 及其关闭按钮的代码。
UIPickerView *tempPickerView = [[[UIPickerView alloc] initWithFrame:
CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -32, 52, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:@"week.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:@selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[tempPickerView bringSubviewToFront:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
它看起来像这样:
该按钮位于 pickerView 的左上角,忘记“周”,这只是一个临时名称。
问题是,我无法触摸这个按钮,每次我尝试触摸它时,我只是直接触摸它后面的东西,例如talbeViewCell。
是因为我在 pickerView 框架之外添加了按钮吗?
我的意思是按钮原点的坐标是(0,-32),
这样它就会显示在pickerView上方,坐标有问题吗?
谁能告诉如何解决这个问题?
非常非常感谢!!!
I made a button above a pickerView to dismiss it.
and I think I made it,
here the the code that initiates the pickerView as well as its dismiss button.
UIPickerView *tempPickerView = [[[UIPickerView alloc] initWithFrame:
CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
tempPickerView.showsSelectionIndicator = YES;
tempPickerView.delegate = self;
tempPickerView.dataSource = self;
UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -32, 52, 32)];
[pickerButton setBackgroundImage:[UIImage imageNamed:@"week.png"]
forState:UIControlStateNormal];
[pickerButton addTarget:self action:@selector(hidePicker)
forControlEvents:UIControlEventTouchUpInside];
[tempPickerView addSubview:pickerButton];
[tempPickerView bringSubviewToFront:pickerButton];
[pickerButton release];
[self.view addSubview:tempPickerView];
return tempPickerView;
and it looks like this:
that button is at the upper left cornor of the pickerView, forget about the "week", it is just a temporary name.
Problem is, I can't touch this button, everytime I tried to touch it, I just directly touched things behind it such as the talbeViewCell.
Is is because I added the button outside of the pickerView's frame?
I mean the coordinate of the button's origin is (0, -32),
so that it would be displayed above the pickerView, is there any problems with the coordinate?
Can anyone tell how to fix this problem?
Thanks a lot and a lot!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您已将按钮放置在其父视图的边界之外。 iOS 现在会这样做(经过简化,不是 100% 正确,但你明白了):
UIWindow
对象上的hitTest:withEvent:
方法。hitTest:withEvent:
,并且如果需要,这些子级也会在其子级上调用hitTest:withEvent:
。hitTest:withEvent:
时,它首先查看坐标是否在其自己的边界内 (IIRC)。如果不是,则返回nil
。否则,它会对其子级调用hitTest:withEvent:
。hitTest:withEvent:
文档 明确指出:现在的问题是您的按钮位于其父视图之外。因此,
hitTest:withEvent:
确定触摸坐标不在选取器视图的边界内,并且从不询问其子级(按钮)是否可能被击中。您可以对选择器进行子类化并重写
hitTest:withEvent:
方法来“修复”此问题,但真正的解决方法是移动按钮,使其成为选择器的同级按钮,而不是子按钮。The problem is that you have placed the button outside the bounds of its parent view. iOS now does this (simplified, not 100% correct but you get the deal):
hitTest:withEvent:
method on theUIWindow
object of your app.hitTest:withEvent:
on each of its children, and these each callhitTest:withEvent:
on their children if necessary.hitTest:withEvent:
is called, it first looks whether the coordinate is within its own bounds (IIRC). If it isn't, it returnsnil
. Otherwise it callshitTest:withEvent:
on its children.The
hitTest:withEvent:
documentation explicitly says:The problem now is that your button is outside of its parent view. So
hitTest:withEvent:
determines that the touch coordinate is not inside the bounds of the picker view and never bothers to ask its child (the button) whether it might be hit.You could subclass the picker and override the
hitTest:withEvent:
method to "fix" this, but the real fix is to move the button so its a sibling of the picker, not a child.我认为这可能是由
UIPickerView
的命中测试方法引起的。如果触摸超出其范围,它可能会从pointInside:withEvent:
返回NO
。头文件(UIView.h)注释说由于您将按钮放置在
UIPickerView
的边界之外,这很可能就是原因。作为解决方法,您可以将按钮插入选择器的超级视图中。
I think this may be caused by
UIPickerView
's hit-testing method. It may returnNO
frompointInside:withEvent:
if the touch is outside its bounds. The header file (UIView.h) comment saysSince you place the button outside the bounds of
UIPickerView
, that may well be the reason.As a workaround, you can insert the button into the picker's superview.