2

こんにちは、UIBarButtonItem の色合いであると思われる「塗りつぶし」を変更する方法を見つけようとしていますが、外観または外観を使用してそれを実行しようとすると、ContainedIn が機能せず、アイコンが青く表示され続けます:

ここに画像の説明を入力

ボタン自体をカスタマイズすると、色合いを変更できて機能しますが、すべてのボタンの外観を変更したいと思います。スタイリングを行うコードは次のとおりです。誰かがこの種のことを行う方法のヒントやヒントを教えてもらえますか?

-(void)applyStyle {

    [self styleUIButtons];


    UIImage *navigationBarBackground = [[UIImage imageNamed:@"base_nav_bar" ] stretchableImageWithLeftCapWidth:0 topCapHeight:0];

    [[UINavigationBar appearance]setBackgroundImage:navigationBarBackground forBarMetrics:UIBarMetricsDefault];

    [[UINavigationBar appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName: self.mainNavigationBarTextColor}];

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName: self.mainNavigationBarTextColor} forState:UIControlStateNormal];

    [[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleColor:self.mainNavigationBarTextColor forState:UIControlStateNormal];

    UIImage* barButtonImage = [self createSolidColorImageWithColor:[UIColor colorWithWhite:1.0 alpha:0.1] andSize:CGSizeMake(10, 10)];

    [[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [[UIButton appearanceWhenContainedIn:[UINavigationBar class], nil] setBackgroundImage:barButtonImage forState:UIControlStateNormal];

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:self.mainNavigationBarIconColor  ];


    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont boldSystemFontOfSize:12], NSFontAttributeName,
                                [UIColor grayColor], NSForegroundColorAttributeName,

                                nil];
    [[UISegmentedControl appearance]setTitleTextAttributes:attributes forState:UIControlStateNormal];





}

-(UIImage*)createSolidColorImageWithColor:(UIColor*)color andSize:(CGSize)size{

    CGFloat scale = [[UIScreen mainScreen] scale];
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGRect fillRect = CGRectMake(0,0,size.width,size.height);
    CGContextSetFillColorWithColor(currentContext, color.CGColor);
    CGContextFillRect(currentContext, fillRect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (void) styleUIButtons {
    UIImage *buttonNormalBg = [[UIImage imageNamed:@"button_normal" ] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
    UIImage *buttonSelectedBgb = [[UIImage imageNamed:@"button_selected" ] stretchableImageWithLeftCapWidth:0 topCapHeight:0];

    id appearance = [UIButton appearance];

    [appearance setTintColor:self.mainNavigationBarTextColor];
    [appearance setBackgroundImage:buttonNormalBg forState:UIControlStateNormal];
    [appearance setBackgroundImage:buttonSelectedBgb forState:UIControlStateHighlighted];
}
4

1 に答える 1

10

iOS 7 の UIBarButtonItem で元の画像の色を維持するには、imageWithRenderingMode: UIImageRenderingModeAlwaysOriginalを使用して、以下のコードのように設定してみてください。

UIImage *buttonImage = [UIImage imageNamed:@"myImage"];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[buttonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                                              style:UIBarButtonItemStylePlain
                                                             target:self
                                                             action:@selector(action:)];
self.navigationItem.rightBarButtonItem = barButton;
于 2013-10-16T05:36:41.420 に答える