UIBarButtonItem将图标填充更改为白色ios

时间:2013-10-15 03:36:23

标签: ios ios7 styling uiappearance

您好我一直在试图弄清楚如何更改“填充”,我认为它是UIBarButtonItem的色调,但是当我尝试使用外观或外观时,如果不能正常工作,那么请继续向我提供图标蓝色:

enter image description here

我希望它可能是白色的,当我自定义按钮本身时,我可以改变色调颜色并且它可以工作,但我想用所有按钮的外观来做。这是我做样式的代码,如果有人可以给我一个提示或提示如何做这种事情?

-(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];
}

1 个答案:

答案 0 :(得分: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;