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 回答