UITableView tableHeaderView 中的 UIButton 没有响应触摸 [英] UIButton in UITableView tableHeaderView not responding to touches

查看:36
本文介绍了UITableView tableHeaderView 中的 UIButton 没有响应触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我觉得自己像个白痴,我无法完成这项工作,而且关于 SO 的其他任何答案都没有帮助我.

Ok I feel like an idiot that I can't get this working, and none of the other answers on SO are getting me anywhere.

我有一个非常简单的 UIView,我在 .xib 中定义了它,并作为我的 UITableViewControllertableHeaderView 加载:

I have a very simple UIView that I have defined in a .xib and am loading as the tableHeaderView of my UITableViewController:

UIView* headerView = [[NSBundle mainBundle] loadNibNamed:@"ProfileHeaderView" owner:self options:nil][0];
self.tableView.tableHeaderView = headerView;

标头 .xib 的文件所有者设置为我的控制器类,到目前为止一切正常.我的插座连接正确,我可以将标签设置为正确的值等.

The file's owner of the header .xib is set to my controller class, and so far this much works fine. My outlets are connecting properly and I can set my labels to their correct values, etc.

但是,当我向 headerView 添加 UIButton 时,无论我尝试什么,它都不会响应触摸.

But, when I added a UIButton to my headerView, it is not responding to touches no matter what I try.

我已经确认标题和按钮的框架是正确的,并且都将 userInteractionEnabled 设为 YES.

I've confirmed that the frames of the header and of the button are correct and both have userInteractionEnabled as YES.

我终其一生都无法弄清楚为什么这不起作用.我真的没有在这里做任何我能想到的棘手的事情.

I cannot for the life of me figure out why this isn't working. I am really not doing anything tricky here that I can think of.

以下是一些调试信息:

po self.tableView.tableHeaderView
<UIView: 0x17ef5420; frame = (0 0; 320 194); autoresize = W+H; layer = <CALayer: 0x17eda4a0>>

po self.loginButton
<UIButton: 0x17ec1630; frame = (80 164; 160 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x17eec760>>

更新:我发现了一些新证据.以上记录在 viewDidLoad 中,并且框架具有正确的值.但是,如果我在 tableView:didSelectRowAtIndexPath: 中设置断点,我注意到不知何故我的标题视图框架高度已更改为 130 像素!

Update: I discovered some new evidence. The above were logged inside viewDidLoad, and the frames have their correct values. However, if I set a breakpoint in tableView:didSelectRowAtIndexPath:, I notice that somehow my header view frame height has changed to 130 px!

po self.profileHeaderView
<UIView: 0x15eb6790; frame = (0 0; 320 130); autoresize = W+H; layer = <CALayer: 0x15eb67f0>>

我不知道是什么原因造成的!当然,既然我知道这一点,如果我在 viewDidAppear: 中明确地将框架的高度设置回 194,它就可以正常工作.但我显然想首先修复任何改变我的框架的东西.

I don't know what is causing this! Of course now that I know this, if I explicitly set the frame's height back to 194 in viewDidAppear: it works fine. But I would obviously like to fix whatever is changing my frame in the first place.

标题仍然可以正确绘制,并且在渲染时是正确的高度(194 像素),但是由于某种原因,它的框架在某个时候发生了变化,这导致按钮落在其框架之外,因此不会接收触摸.

The header still draws properly, and is the correct height (194 px) when it is rendered, but for some reason its frame is getting changed at some point, which is causing the button to fall outside of its frame and thus not receive touches.

什么会像这样改变标题的框架?我根本没有设置任何限制.

What would be changing the header's frame like this? I don't have any constraints set up on it at all.

推荐答案

试试这个,

    [button sendActionsForControlEvents:UIControlEventTouchUpInside];

如果这正确调用了您的目标/选择器,那么问题在于您的视图层次结构.如果不是,那么问题是您没有正确编码按钮的目标/选择器.

If this properly calls your target / selector then the problem is with your view hierarchy. If not then problem is that you haven't properly coded the target / selector for the button.

这篇关于UITableView tableHeaderView 中的 UIButton 没有响应触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆