简体   繁体   中英

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:

  • Setting the tint color on the label
  • Setting the label.userInteractionEnabled = NO
  • Setting the label.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed to all available values (none of which helped)
  • Subclassing UIButton and drawing the # cart items during drawRect
  • During the view controller presentation, finding the label in the navigationItem.rightBarButtonItem.customView hierarchy and setting the tintAdjustmentMode manually.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM