How to disable “highlight subviews” message for UI

2019-03-13 02:26发布

问题:

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.

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

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?

回答1:

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

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

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.

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 is available after IOS4+
  • _updateHighlightColorsForView is the UITableView's method, responsible for highlighting all children


回答2:

I have a class which inherits from UITableViewCell, so i fixed it overriding setSelected:animated like this:

   - (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];
   }


回答3:

I solved this problem by overriding -(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated like this (only works if the subview is a subclass of 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)
}


回答4:

Use UITableViewCellSelectionStyleNone on the table cells.

See the apple API documentation.



回答5:

Recently I was looking for the same approach an to be honest I couldn't find a way to programmatically do this, so I kind of cheated my way out of this one.

Since highlighting respects images I ended up using a UIImageView filing the entire frame of my UIView and assigning a blank image to that and draw the rest of the view above the UIImageView. Your UIView will still be highlighted but you won't be able to see it because there will be a blank image above!

I hope this helps, I know this is probably not the best approach but got me the results I required



回答6:

Override -(void)setHighlighted:(BOOL)highlight; method in your UIControl subclass so that it does nothing. In the case of a UIButton subclass, you can do:

- (void)setHighlighted:(BOOL)highlighted {
    return;
}


回答7:

I didn't want any of the subviews to be highlighted. My solution was to set the selectionstyle to UITableViewCellSelectionStyleNone and to mimic the default selection behaviour as follows (overriding setSelected on a custom sub class of UITableViewCell):

- (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];
}


回答8:

May be we can change the highlighted state of subviews to match their default view.

That way even if it changes to highlighted state it will look to be in default state.

Not sure if it works, can you describe subviews more.



回答9:

I think you need to either disable the subviews (likely undesirable) or subclass the subviews to override this behavior.



回答10:

Maybe if you subclass your subview and override the setHighlighted method to do nothing or override the highlighted method to return always NO.

But UIView doesn't have the highlighted state, what kind of UIView childs you add to your cell ?



回答11:

So maybe a kind of this :

- (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;
}


回答12:

Just put yours views inside a UIButton and they aren't updating on highlighting. You can't do that with xib of storyboard only on runtime.

UIButton *button = [[UIButton alloc] initWithFrame:0, 0, cellWidth, cellHeight];
[button addSubview:anyView]; // This view don't change his background color
[cell addSubview:button];