【问题标题】:UILabel as subview of UIBarButtonItem.customView not dimming when presenting view controllerUILabel 作为 UIBarButtonItem.customView 的子视图在呈现视图控制器时不会变暗
【发布时间】:2015-01-29 00:31:27
【问题描述】:

我遇到了一个问题,即当视图控制器以模态方式呈现时,导航栏中的 UILabel 未正确着色。

UILabel在导航栏中,作为一个UIButton的子视图,它是UIBarButtonItem的子视图,也就是导航控制器的rightBarButtonItem;查看层次结构:

rightBarButtonItem -UIBarButtonItem --UIButton

需要明确的是,除了呈现模式期间的标签色调外,一切正常。在显示视图控制器之前,购物车被染成蓝色,包含 # 购物车项目的标签也是如此。当模态出现时,购物车图像变暗,但标签保持蓝色。

我会发布图片,但我没有足够的声誉,抱歉。

我尝试过的:

  • 在标签上设置色调颜色
  • 设置label.userInteractionEnabled = NO
  • label.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed 设置为所有可用值(没有任何帮助)
  • 子类化 UIButton 并在 drawRect 期间绘制 # 个购物车项目
  • 在视图控制器演示期间,在 navigationItem.rightBarButtonItem.customView 层次结构中找到标签并手动设置 tintAdjustmentMode。

没有任何效果。我没有想法......

这是我创建 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;
}

有什么想法吗?

【问题讨论】:

  • 你试过直接改textColor吗?

标签: ios uinavigationitem rightbarbuttonitem uibuttonbaritem


【解决方案1】:

我能想到的最佳解决方案是在模态视图控制器出现时禁用标签。当模式被关闭时,我再次用一个新的启用标签替换工具栏菜单项(通过调用getCartBarButtonItemWithDelegate)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2016-06-12
    • 2014-01-28
    • 2014-11-17
    • 1970-01-01
    相关资源
    最近更新 更多