我在iPad上的表格视图单元格中放置了按钮,这有一个奇怪的问题。
以下是两个更好理解的屏幕截图(按钮位于右侧,背景颜色为白色):
iPhone没问题,但在iPad上此按钮没有响应。 由于绘制正确,定位似乎是正确的。
这是我的自定义UITableViewCell的代码:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setContentMode:UIViewContentModeCenter];
[rightButton setBackgroundColor:[UIColor whiteColor]];
int xPos = buttonWidth-rightButton.frame.size.width;
[rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)];
[self.contentView addSubview:rightButton];
//buttonWidth = 310, buttonHeight = 60
图像和目标由另一个类设置:
[cell.rightButton setImage:[UIImage imageNamed:@"options.png"] forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
我还在将所有元素放入单元格后尝试了这个
[self.contentView bringSubviewToFront: rightButton];
确保按钮位于最顶端。
没用。
有关于此的任何想法吗?
我仍然无法找到解决方案。
我在此期间做了什么:
还有其他想法吗?
答案 0 :(得分:0)
首先尝试这个:
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setContentMode:UIViewContentModeCenter];
[rightButton setBackgroundColor:[UIColor whiteColor]];
int xPos = buttonWidth-rightButton.frame.size.width;
[rightButton setFrame:CGRectMake(xPos, 0, buttonHeight, buttonHeight)];
[self.contentView addSubview:rightButton];
[self.contentView bringSubviewToFront: rightButton]
[self.contentView bringSubviewToFront: rightButton];
rightButton.userInteractionEnabled = YES;
[cell.rightButton setImage:[UIImage imageNamed:@"options.png"] forState:UIControlStateNormal];
[cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightButton setUserInteractionEnabled:YES];
或者您也可以在按钮上添加点击手势,它会解决问题: 而不是这一行,
[cell.rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
写这个,
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showOptions:)];
cell.rightButton addGestureRecognizer:tap];
[cell.rightButton setUserInteractionEnabled:YES];
答案 1 :(得分:0)
好的,我找到了一些东西:
对于iPad,按钮仅在水平放置在前320个像素之间时才会响应。
以下是调试器在触摸按钮时所说的内容(放置在右侧):
(lldb) po touches
(NSSet *) $1 = 0x08249cd0 {(
<UITouch: 0x82c63d0> phase: Began tap count: 1 window: <UIWindow: 0x8266ca0;
frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM;
layer = <UIWindowLayer: 0x8266d60>> view: <SBMultiCell: 0x82cb8a0; baseClass = UITableViewCell;
frame = (0 0; 744 93); autoresize = W; layer = <CALayer: 0x82cb830>>
location in window: {713, 123} previous location in window: {713, 123}
location in view: {701, 44} previous location in view: {701, 44}
)}
(lldb) po rightButton
(UIButton *) $2 = 0x082cd500 <UIButton: 0x82cd500;
frame = (658 0; 86 86); opaque = NO; layer = <CALayer: 0x82cd5e0>>
触摸位置恰好位于按钮尺寸之间,并且所有内容都正确绘制。正如我之前所写的那样,没有重叠元素之类的东西......