如何在iOS SDK中禁用UIView / UIViewController的“highlight subviews”消息?
我想在点击UITableViewCell
时使用默认高亮。 不过,我不希望自定义子视图(及其子视图)接收消息来更新其突出显示的状态,因此会中断backgroundColor
属性。
编辑
“subview”是指任何UIView
子类,而不仅仅是UITableViewCell
。
也许这个假设的情况将更好地阐明我在找什么 :我有一个UITableViewCell 。 叫它c 。 然后我添加一个UIView(称为v )作为c的子视图。 当我点击c时 ,我想要c突出显示(标准的蓝色背景,白色的字体颜色),但我不想v突出显示。 我如何做到这一点?
首先, UITableView会激发所有的子视图,并向它们发送突出显示的消息。
所以,即使你把UILabel放到你的视图中,不pipe它多深,它都会遍历所有视图(通过使用子视图属性)。
一个解决scheme可以是(这是IOS4 +),覆盖子视图属性,并作弊tableview的突出显示function,我们没有任何子视图。 要做到这一点,我们需要确定调用者,如果它是tableview的高亮方法,我们应该不返回任何子视图。
我们可以创build一个简单的UIView子类并覆盖下面的子视图 。
- (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的方法,负责突出显示所有的孩子
我有一个inheritance自UITableViewCell
的类,所以我修正了覆盖setSelected:animated
像这样的setSelected:animated
:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; //Reset highlighted status to all the childs that i care for. [self.someChild setHighlighted:NO]; [self.someOtherChild setHighlighted:NO]; }
我解决了这个问题,覆盖-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
像这样的-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
(只有当子视图是UIControl的子类):
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { BOOL subviewWasEnabled = mySubview.enabled; //check if the view is enabled mySubview.enabled = NO; //disable it anyways [super setHighlighted:highlighted animated:animated]; mySubview.enabled = subviewWasEnabled; //enable it again (if it was enabled) }
在表格单元格上使用UITableViewCellSelectionStyleNone。
请参阅Apple API文档 。
最近我一直在寻找相同的方法,说实话我找不到一种方法来编程这样做,所以我有点欺骗我的出路。
由于突出显示尊重图像我结束了使用UIImageView
提交UIView
的整个框架,并分配一个空白图像,并绘制UIImageView
上方的其余视图。 你的UIView
仍然会被高亮显示,但是你将无法看到它,因为上面会有一个空白的图像!
我希望这有助于,我知道这可能不是最好的办法,但得到了我需要的结果
覆盖-(void)setHighlighted:(BOOL)highlight;
方法在你的UIControl
子类中,所以它什么都不做。 在UIButton
子类的情况下,你可以这样做:
- (void)setHighlighted:(BOOL)highlighted { return; }
我不希望任何子视图被突出显示。 我的解决scheme是将selectionstyle设置为UITableViewCellSelectionStyleNone并按照以下方式模仿默认的select行为(在UITableViewCell的自定义子类上重写setSelected):
- (void)setSelected:(BOOL)selected animated:(BOOL)animated { int backgroundViewTag = 888; UIView *backgroundView = [self viewWithTag:backgroundViewTag]; if (selected) { if (!backgroundView) { backgroundView = [[[UIView alloc] initWithFrame:self.bounds] autorelease]; backgroundView.tag = backgroundViewTag; backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; backgroundView.backgroundColor = [UIColor blueColor]; backgroundView.alpha = 0.0; [self insertSubview:backgroundView atIndex:0]; } if (animated) { [UIView animateWithDuration:0.5 animations:^{ backgroundView.alpha = 1.0; }]; } else { backgroundView.alpha = 1.0; } } else if (backgroundView) { if (animated) { [UIView animateWithDuration:0.5 animations:^{ backgroundView.alpha = 0.0; } completion:^(BOOL finished) { }]; } else { backgroundView.alpha = 0.0; } } [super setSelected:selected animated:animated]; }
可能是我们可以改变子视图的突出显示的状态,以匹配他们的默认视图。
这样,即使它变成突出显示的状态,它将看起来处于默认状态。
不知道它是否有效,你可以更多地描述子视图。
我认为你需要禁用子视图(可能是不可取的),或子类的子视图覆盖这种行为。
也许如果你subview你的子视图,并重写setHighlighted方法什么都不做或重写突出显示的方法总是返回NO。
但UIView没有突出显示的状态,你添加到你的单元格是什么样的UIView孩子?
所以也许是这样的一种:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; m_colors = [[NSMutableArray alloc] init]; for (UIView *view in [cell.contentView subviews]) { [m_colors addObject:view.backgroundColor]; } return indexPath; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; for (int x = 0; x < [m_colors count]; ++x) { [[[cell.contentView subviews] objectAtIndex:x] setBackgroundColor:[m_colors objectAtIndex:x]]; } [m_colors release]; m_colors = nil; }
只要把你的意见在一个UIButton ,他们不更新突出显示。 只有在运行时才能用故事板的xib执行此操作。
UIButton *button = [[UIButton alloc] initWithFrame:0, 0, cellWidth, cellHeight]; [button addSubview:anyView]; // This view don't change his background color [cell addSubview:button];