iOS 7.0 UITableView backgroundView 中的控件禁用了用户交互

Posted

技术标签:

【中文标题】iOS 7.0 UITableView backgroundView 中的控件禁用了用户交互【英文标题】:iOS 7.0 User interaction disabled for controls inside UITableView backgroundView 【发布时间】:2013-12-02 15:23:23 【问题描述】:

我将自定义视图设置为 UITableview 的背景视图。当 tableview 为空白时,它用于执行一些操作。 我在视图中有一些按钮,并且每个按钮都与一个动作相关联 对于 ios 7,在 backgroundView 中的按钮上设置的操作不会被调用。这似乎在 backgroundView 上禁用了交互 这是 iOS 7 的问题。还有其他人面临同样的问题吗?

【问题讨论】:

我认为,这是一个“已知问题”,请在此处查看雷达:openradar.appspot.com/14707569 【参考方案1】:

在背景视图前面有一个UITableViewWrapperView 视图,用于拦截交互。你不能只使用表格的tableHeaderView 属性吗?

【讨论】:

使用 tableHeaderView 可以防止您对视图底部使用约束来布置内容,例如在空表中垂直居中。【参考方案2】:

首先,将可触摸项目添加到 tableView 背景视图是一种不好的方法。 其次,我不确定我的修复是否适用于您的情况。

尝试在您的 BackgroundView 类中实现以下方法:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event

    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    
        [self.superview bringSubviewToFront:self];
    
    return hitView;


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event

    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    
        for (UIView *view in self.subviews)
        
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        
    
    return isInside;

通过覆盖这些方法,您可以使 backgroundView 的所有子视图以及 backgroundView 本身具有触摸感应。

干杯!


更新:

抱歉,这行不通。 BackgroundView 位于单元格本身的视图后面,因此它不会接收到触摸。

【讨论】:

【参考方案3】:

正如我在评论中指出的,这是 iOS7 中的一个已知问题,请参阅雷达 (http://openradar.appspot.com/14707569)。

但我的解决方案或解决方法是在表格顶部实现一个“代理”视图,该视图提供了一个协议,将 hitTest 转发给实现该协议的委托。

EventFixBackrgoundView.h

@protocol EventFixBackrgoundViewDelegate <NSObject>
- (UIView *)eventFixHitTest:(CGPoint)point withEvent:(UIEvent *)event;
@end

@interface EventFixBackrgoundView:UIView
@property (nonatomic, weak) id <EventFixBackrgoundViewDelegate> delegate;
@end

EventFixBackrgoundView.m

#import "EventFixBackrgoundView.h"
@implementation EventFixBackrgoundView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

    if (self.delegate && [self.delegate respondsToSelector:@selector(eventFixHitTest:withEvent:)])
    
        return [self.delegate eventFixHitTest:point withEvent:event];
    
    return [super hitTest:point withEvent:event];

@end

【讨论】:

以上是关于iOS 7.0 UITableView backgroundView 中的控件禁用了用户交互的主要内容,如果未能解决你的问题,请参考以下文章

在 tableHeaderView iOS 7 中使用 UISearchBar 访问错误

MKMapview 在模拟器的 iOS-7.0 中不显示地图

设备不会运行错误 iOS 9 Xcode 7.0

隐藏状态栏 iPad iOS 7.0

iOS 7.0 中的水平 CAGradientLayer

如何使用 xcode 6.4 支持 iOS 7.0?