我遇到一个问题,当视频控制器以模态方式显示时,导航栏中的UILabel没有正确着色。
UILabel位于导航栏中,作为UIButton的子视图,UIButton是UIBarButtonItem的子视图,它是导航控制器的rightBarButtonItem;查看层次结构:
rightBarButtonItem -UIBarButtonItem --UIButton <-- this is UIButtonTypeSystem, with a cart image. Tinting properly. ---UILabel <-- this is the # of items in the cart. Not tinting.
要清楚,一切正常,除了在呈现的模态中的标签色调。在显示视图控制器之前,购物车是蓝色的,包含#购物车项目的标签也是如此。显示模态时,购物车图像变暗,但标签保持蓝色。
我发布了图片,但我没有足够的声誉,抱歉。
我尝试过:
label.userInteractionEnabled = NO
label.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed
设置为所有可用值(均未提供帮助)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;
}
有什么想法吗?
答案 0 :(得分:0)
我能提出的最佳解决方案是在模态视图控制器出现时禁用标签。当模态被解除时,我再次使用新的启用标签替换工具栏菜单项(通过调用getCartBarButtonItemWithDelegate
)。
这样我就不必尝试匹配应该的颜色。此外,如果链接(和禁用的链接)颜色发生变化,应该确保iOS的未来版本会适当地为链接着色。