UIButton 在 iOS7 中点击时不显示高亮 [英] UIButton not showing highlight on tap in iOS7
问题描述
我查看了大量有关类似内容的帖子,但没有一个完全匹配或解决此问题.从 iOS 7 开始,每当我将 UIButton
添加到 UITableViewCell
或什至页脚视图时,它都可以正常"工作,这意味着它会收到目标操作,但不会显示当您点击 UIButton
时通常会发生的小亮点.它使 UI 看起来很时髦,但不显示按钮对触摸的反应.
I've looked at a ton of posts on similar things, but none of them quite match or fix this issue. Since iOS 7, whenever I add a UIButton
to a UITableViewCell
or even to the footerview it works "fine", meaning it receives the target action, but it doesn't show the little highlight that normally happens as you tap a UIButton
. It makes the UI look funky not showing the button react to touch.
我很确定这算作 iOS7 中的一个错误,但有没有人找到解决方案或可以帮我找到一个 :)
I'm pretty sure this counts as a bug in iOS7, but has anyone found a solution or could help me find one :)
我忘了提到如果我长按按钮它会突出显示,但不会像添加到标准视图那样快速点击.
I forgot to mention that it will highlight if I long hold on the button, but not a quick tap like it does if just added to a standard view.
代码:
创建按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize:14];
button.titleLabel.textColor = [UIColor blueColor];
[button setTitle:@"Testing" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchDown];
button.frame = CGRectMake(0, 0, self.view.frame.size.width/2, 40);
我测试过的东西:
//移除 UITableView
上的手势识别器,以防它们挡路.
//Removing gesture recognizers on UITableView
in case they were getting in the way.
for (UIGestureRecognizer *recognizer in self.tableView.gestureRecognizers) {
recognizer.enabled = NO;
}
//从单元格中移除手势
//Removing gestures from the Cell
for (UIGestureRecognizer *recognizer in self.contentView.gestureRecognizers) {
recognizer.enabled = NO;
}
//这显示了轻微的触感,但这不是想要的样子
//This shows the little light touch, but this isn't the desired look
button.showsTouchWhenHighlighted = YES;
推荐答案
在那个 tableview 中你只需添加这个属性.
In that tableview you just add this property.
tableview.delaysContentTouches = NO;
并在您启动您刚刚添加以下代码的单元格后添加 cellForRowAtIndexPath.单元格的结构在 iOS 6 和 iOS 7 中明显不同.
iOS 7 我们有一个控件 UITableViewCellScrollView 在 UITableViewCell 和 content View 之间.
And add in cellForRowAtIndexPath after you initiate the cell you just add below code. The structure of the cell is apparently different in iOS 6 and iOS 7.
iOS 7 we have one control UITableViewCellScrollView In between UITableViewCell and content View.
for (id obj in cell.subviews)
{
if ([NSStringFromClass([obj class]) isEqualToString:@"UITableViewCellScrollView"])
{
UIScrollView *scroll = (UIScrollView *) obj;
scroll.delaysContentTouches = NO;
break;
}
}
这篇关于UIButton 在 iOS7 中点击时不显示高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!