Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UILabel作为UIBarButtonItem的子视图。显示视图控制器时,customView不变暗_Ios_Uinavigationitem_Rightbarbuttonitem_Uibuttonbaritem - Fatal编程技术网

Ios UILabel作为UIBarButtonItem的子视图。显示视图控制器时,customView不变暗

Ios UILabel作为UIBarButtonItem的子视图。显示视图控制器时,customView不变暗,ios,uinavigationitem,rightbarbuttonitem,uibuttonbaritem,Ios,Uinavigationitem,Rightbarbuttonitem,Uibuttonbaritem,我有一个问题,当视图控制器以模式显示时,导航栏中的UILabel没有正确着色 UILabel位于导航栏中,作为UIButton的子视图,UIButton是UIBarButtonItem的子视图,UIButtonItem是导航控制器的rightBarButtonItem;视图层次结构: rightBarButtonItem -UIBarButtonItem --UIButton <-- this is UIButtonTypeSystem, with a cart image. Tintin

我有一个问题,当视图控制器以模式显示时,导航栏中的UILabel没有正确着色

UILabel位于导航栏中,作为UIButton的子视图,UIButton是UIBarButtonItem的子视图,UIButtonItem是导航控制器的rightBarButtonItem;视图层次结构:

rightBarButtonItem -UIBarButtonItem --UIButton <-- this is UIButtonTypeSystem, with a cart image. Tinting properly. ---UILabel <-- this is the # of items in the cart. Not tinting.
有什么想法吗?

我能想到的最好的解决方案是在模态视图控制器出现时禁用标签。当模式被解除时,我再次调用getCartBarButtonimWithDelegate,用一个新的、启用的标签替换工具栏菜单项

这样我就不必试着去匹配它应该的颜色。此外,如果链接和禁用链接的颜色发生变化,这将确保iOS的未来版本将适当地为链接着色。

您是否尝试过直接更改textColor?
+(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;
}