UIButton在iOS7中没有显示突出显示

Eri*_*ric 68 iphone objective-c uitableview ios ios7

我看过很多关于类似事情的帖子,但没有一个匹配或解决这个问题.从iOS 7开始,每当我添加UIButton到一个UITableViewCell或甚至是一个footerview它都工作"很好",这意味着它接收目标动作,但它没有显示通常在点击时发生的小亮点UIButton.它使UI看起来很时髦而不显示按钮对触摸的反应.

我很确定这是iOS7中的一个错误,但是有人找到了解决方案或者可以帮我找到一个:)

编辑:我忘了提到它会突出显示我是否长按按钮,但如果只是添加到标准视图就不会像它那样快速点击.

码:

创建按钮:

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);
Run Code Online (Sandbox Code Playgroud)

我测试的东西:

//删除手势识别器UITableView,以防他们挡路.

for (UIGestureRecognizer *recognizer in self.tableView.gestureRecognizers) {
   recognizer.enabled = NO;
}
Run Code Online (Sandbox Code Playgroud)

//从Cell中删除手势

for (UIGestureRecognizer *recognizer in self.contentView.gestureRecognizers) {
       recognizer.enabled = NO;
    }
Run Code Online (Sandbox Code Playgroud)

//这显示了轻微的触摸,但这不是理想的外观

button.showsTouchWhenHighlighted = YES;
Run Code Online (Sandbox Code Playgroud)

小智 87

在该tableview中,您只需添加此属性即可.

tableview.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)

并在启动单元格后添加cellForRowAtIndexPath,只需添加以下代码即可.在iOS 6和iOS 7中,单元格的结构明显不同
.iOS 7我们在UITableViewCell和内容View之间有一个控件UITableViewCellScrollView.

for (id obj in cell.subviews)
{
    if ([NSStringFromClass([obj class]) isEqualToString:@"UITableViewCellScrollView"])
    {
        UIScrollView *scroll = (UIScrollView *) obj;
        scroll.delaysContentTouches = NO;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这个代码在iOS8中不起作用,因为UITableViewCellScrollView已从UITableViewCell视图层次结构中删除. (5认同)
  • 你是天才,谢谢!我编辑了你的子视图循环,使它更具未来性.发布编辑,所以一旦获得批准,将有希望帮助其他人 (3认同)
  • @RyanRomanchuk我用它有多个按钮.我知道如果滚动的开头(你点击的地方)是一个按钮,这会停止滚动.这是因为现在触摸被发送到按钮而不是滚动视图.我仍然这样做,因为我认为显示按钮突出显示比使滚动工作更重要,即使他们开始按钮上的滚动.这可能只是在我的情况下 (2认同)

Rom*_* B. 40

从iOS 8开始,我们需要将相同的技术应用于UITableView子视图(表包含隐藏的UITableViewWrapperView滚动视图).不再需要迭代UITableViewCell子视图.

for (UIView *currentView in tableView.subviews) {
    if ([currentView isKindOfClass:[UIScrollView class]]) {
        ((UIScrollView *)currentView).delaysContentTouches = NO;
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个答案应与这个问题联系起来.

  • 在iOS8上确认. (4认同)

Eri*_*ric 27

我试图将此添加到已接受的答案中,但它从未经历过.这是关闭单元delayedContentTouches属性的一种更安全的方法,因为它不查找特定的类,而是查找响应选择器的任何内容.

在细胞中:

for (id obj in self.subviews) {
     if ([obj respondsToSelector:@selector(setDelaysContentTouches:)]) {
          [obj setDelaysContentTouches:NO];
     }
}
Run Code Online (Sandbox Code Playgroud)

在TableView中:

self.tableView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)

  • 这比接受的答案要好.我要补充的一点是,最好在返回单元格之前添加它,而不是在实例化之后,因为设置单元格可以重置`setDelaysContentTouches` (3认同)

Que*_*tin 18

对于适用于iOS7和iOS8的解决方案,请创建自定义UITableView子类和自定义UITableViewCell子类.

使用该样品UITableViewinitWithFrame:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        // iterate over all the UITableView's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewWrapperView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
            {
                // this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

使用该样品UITableViewCellinitWithStyle:reuseIdentifier:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        // iterate over all the UITableViewCell's subviews
        for (id view in self.subviews)
        {
            // looking for a UITableViewCellScrollView
            if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                // this test is here for safety only, also there is no UITableViewCellScrollView in iOS8
                if([view isKindOfClass:[UIScrollView class]])
                {
                    // turn OFF delaysContentTouches in the hidden subview
                    UIScrollView *scroll = (UIScrollView *) view;
                    scroll.delaysContentTouches = NO;
                }
                break;
            }
        }
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)


Rap*_*nto 16

我用来解决问题的方法是使用以下代码的UIButton类别:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];


    [NSOperationQueue.mainQueue addOperationWithBlock:^{ self.highlighted = YES; }];
}


- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];

    [self performSelector:@selector(setDefault) withObject:nil afterDelay:0.1];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    [self performSelector:@selector(setDefault) withObject:nil afterDelay:0.1];
}


- (void)setDefault
{
    [NSOperationQueue.mainQueue addOperationWithBlock:^{ self.highlighted = NO; }];
}
Run Code Online (Sandbox Code Playgroud)

当我在UITableViewCell中按下按钮时,按钮会正确反应,而我的UITableView在delaysContentTouches没有强制的情况下表现正常.

  • 类别?你确定?:) (2认同)

bra*_*ipt 10

这是罗曼B在Swift 2中的答案:

for view in tableView.subviews {
    if view is UIScrollView {
        (view as? UIScrollView)!.delaysContentTouches = false
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么没人试过这个,但是对我有用.问题是为什么tableview(它是一个scrollview子类本身)内部有另一个scrollview.Apple:| (3认同)

Gan*_*ank 5

    - (void)viewDidLoad
{

    [super viewDidLoad];


    for (id view in self.tableView.subviews)
    {
        // looking for a UITableViewWrapperView
        if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewWrapperView"])
        {
            // this test is necessary for safety and because a "UITableViewWrapperView" is NOT a UIScrollView in iOS7
            if([view isKindOfClass:[UIScrollView class]])
            {
                // turn OFF delaysContentTouches in the hidden subview
                UIScrollView *scroll = (UIScrollView *) view;
                scroll.delaysContentTouches = NO;
            }
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


skg*_*skg 5

我在 UITableViewCell 中的纯文本 UIButton 遇到了类似的问题,没有在触摸时突出显示。对我来说修复它的是将 buttonType 从自定义更改回系统。

将 delaysContentTouches 设置为 NO 对同一 UITableViewCell 中的纯图像 UIButton 起到了作用。

self.tableView.delaysContentTouches = NO;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明