iosuinavigationitemrightbarbuttonitemuibuttonbaritem

UILabel as subview of UIBarButtonItem.customView not dimming when presenting view controller


I have an issue where UILabel in the navigation bar is not being tinted properly when a view controller is presented modally.

The UILabel is in the navigation bar, as a child view of a UIButton, which is a child view of the UIBarButtonItem, which is the rightBarButtonItem of the navigation controller; View hierarchy:

rightBarButtonItem
-UIBarButtonItem
--UIButton <-- this is UIButtonTypeSystem, with a cart image. Tinting properly.
---UILabel <-- this is the # of items in the cart. Not tinting.

To be clear, everything works fine except the label tint during a presented modal. Before the view controller is presented, the cart is tinted blue and so is the label containing the # of cart items. When the modal is presented, the cart image dims, but the label stays blue.

I'd post images, but I do not have enough reputation, sorry.

What I have tried:

Nothing has worked. I'm out of ideas...

Here is the code where I am creating the UIBarButtonItem:

+(UIBarButtonItem*) getCartBarButtonItemWithDelegate:(id)delegate {

    NSInteger cartItems = [[DataHandler sharedInstance]cartQuantity];

    NSString* num = [NSString stringWithFormat:@"%lu", (unsigned long) cartItems];
    NSString* cartImageToUse = @"cart_toolbar_button_icon";
    CGFloat fontSize = 11;
    UILabel *label = nil;

    if(cartItems  > 0) {
        if([num length] > 1) {
            cartImageToUse = @"cartnumbered_toolbar_button2_icon";
            fontSize = 10;
            label = [[UILabel alloc]initWithFrame:CGRectMake(7, -3, 16, 12)];
        } else {
            cartImageToUse = @"cartnumbered_toolbar_button_icon";
            label = [[UILabel alloc]initWithFrame:CGRectMake(7.5, -3, 16, 12)];
        }        

        [label setFont:[UIFont systemFontOfSize:fontSize]];
        [label setText: num ];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
    }

    // attempt at sub classing UIButton and drawing the number of items in the drawRect method
    //CartButton *button =  [CartButton buttonWithType:UIButtonTypeSystem];
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed: cartImageToUse] forState:UIControlStateNormal];
    [button addTarget:delegate action:@selector(handleCartTouch:)forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 25, 21)];

    if(label != nil) {
        [label setTextColor: button.tintColor];                    
        [button addSubview:label];
    }

    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [label release];
    return newBackButton;
}

Any ideas?


Solution

  • The best solution I could come up with was to disable the label when the modal view controller gets presented. When the modal get dismissed, I replace the toolbar menu item (by calling getCartBarButtonItemWithDelegate) again, with a fresh, enabled label.

    This way I didnt have to try and match the color that it should be. Also, this should ensure future versions of iOS will color the link appropriately, should the link (and disabled link) colors ever change.