gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:07 33 4
gpt4 key购买 nike

你好,我一直在想办法改变“填充”,我认为它是 UIBarButtonItem 的色调,但是当我尝试使用外观或 appearanceWhenContainedIn 不起作用时,请继续给我图标蓝色:

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

最佳答案

要在 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;

关于ios - UIBarButtonItem 将图标填充更改为白色 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372873/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com