1

ビュー コントローラーがモーダルに表示されているときに、ナビゲーション バーの UILabel が適切に色付けされないという問題があります。

UILabel は、ナビゲーション コントローラーの rightBarButtonItem である UIBarButtonItem の子ビューである UIButton の子ビューとして、ナビゲーション バーにあります。ビューの階層:

rightBarButtonItem
-UIBarButtonItem
--UIButton <-- これは、カートのイメージを持つ UIButtonTypeSystem です。適切に着色します。
---UILabel <-- これはカート内のアイテムの数です。着色していません。

明確にするために、提示されたモーダル中のラベルの色合いを除いて、すべてが正常に機能します。ビュー コントローラが表示される前は、カートが青く着色されており、カート アイテムの数を含むラベルも青く着色されています。モーダルが表示されると、カートの画像が暗くなりますが、ラベルは青色のままです。

画像を投稿したいのですが、評判が十分ではありません。

私が試したこと:

  • ラベルの色合いの設定
  • の設定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;
}

何か案は?

4

1 に答える 1

0

私が思いついた最善の解決策は、モーダル ビュー コントローラーが表示されたときにラベルを無効にすることでした。モーダルが閉じられたら、ツールバーのメニュー項目を ( を呼び出してgetCartBarButtonItemWithDelegate) 再び新しい有効なラベルに置き換えます。

このようにして、あるべき色に合わせようとする必要はありませんでした。また、これにより、リンク (および無効化されたリンク) の色が変更された場合に、iOS の将来のバージョンでリンク適切に色付けされるようになります。

于 2015-02-20T17:04:44.377 に答える