如何禁用“突出显示子视图” iOS SDK中UIView / UIViewController的消息? [英] How to disable "highlight subviews" message for UIView/UIViewController in iOS SDK?

查看:196
本文介绍了如何禁用“突出显示子视图” iOS SDK中UIView / UIViewController的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在点击时使用 UITableViewCell 上的默认突出显示。但是,我不希望自定义子视图(及其子视图)接收消息以更新其突出显示的状态,从而中断 backgroundColor 属性。

I want to use the default highlight on a UITableViewCell when it is tapped. However, I do not want custom subviews (and their subviews) to receive the message to update their highlighted states and therefore disrupt the backgroundColor property.

编辑

子视图是指任何 UIView 子类,而不仅仅是 UITableViewCell s。

Edit
By "subview" I mean any UIView subclass, not just UITableViewCells.

也许这种假设情况会更好地表达我的意思我正在寻找:我有一个 UITableViewCell 。称之为 c 。然后我添加一个UIView(称之为 v )作为 c 的子视图。当我点按 c 时,我希望 c 突出显示(标准蓝色背景,白色字体颜色),但我不希望 v 突出显示。我如何实现这一目标?

Perhaps this hypothetical situation will better articulate what I'm looking for: I have one UITableViewCell. Call it c. I then add one UIView (call it v) as a subview of c. When I tap c, I want c to become highlighted (standard blue background with white font color), but I do not want v to become highlighted. How do I make this happen?

推荐答案

首先, UITableView 会限制所有子视图,以及发送突出显示的消息。

First of all, UITableView enumarates all the subviews, and sends them highlight messages.

因此,即使您在视图中放置了 UILabel ,无论它有多深,它都会遍历所有视图(通过使用子视图属性) 。

So even if you put a UILabel in your view, no matter how deep it is, it traverses all views (by using subviews property).

一个解决方案可以是(这是IOS4 +),覆盖子视图属性,并且欺骗tableview的突出显示功能,我们没有任何子视图。要做到这一点,我们需要确定调用者,如果它是tableview的高亮方法,我们应该根本不返回任何子视图。

One solution can be (which is IOS4+), overriding subviews property, and cheat tableview's highlight function that we do not have any subviews. To do that we need to determine the caller, and if it is tableview's highlight method, we should return no subviews at all.

我们可以创建一个简单的 UIView 子类,并覆盖子视图,如下所示。

We can create a simple UIView subclass and override subviews like below.

- (NSArray *)subviews{
    NSString* backtrace = [NSString stringWithFormat: @"%@",[NSThread callStackSymbols]];
    if ([backtrace rangeOfString:@"_updateHighlightColorsForView"].location!=NSNotFound)
        return [super subviews];   

    return [[NSArray new] autorelease];
}




  • callStackSymbols 是在IOS4 +之后可用

  • _updateHighlightColorsForView是UITableView的方法,负责突出显示所有孩子

    • callStackSymbols is available after IOS4+
    • _updateHighlightColorsForView is the UITableView's method, responsible for highlighting all children
    • 这篇关于如何禁用“突出显示子视图” iOS SDK中UIView / UIViewController的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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