我正在尝试在导航控制器中设置后退按钮的色调颜色,但没有任何效果。我试过了
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
但它保持默认色调
答案 0 :(得分:35)
我相信barButtonItem是只读的。 (我对此不太确定,如果我错了,有人会纠正我)
要为这些按钮添加色调,我会这样做:
在您的App Delegate中,添加以下代码行:
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour
第一行设置了一个外观代理,所以我相信这也有效,如果你喜欢更少的代码:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
我希望这适合你! (这会更改UIBarButtonItem的所有实例)
答案 1 :(得分:32)
您无法直接更改UINavigationItem的色调颜色。您必须更改导航栏的色调颜色,并且栏按钮将自动更改其颜色。
在viewWillAppear方法
中添加这些行[[self.navigationController navigationBar] tintColor] = [UIColor myColor];
或者您可以创建自定义按钮并将UIBarButtonItem初始化为
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
答案 2 :(得分:24)
这对我有用。
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
这与第二个答案非常相似......
答案 3 :(得分:4)
如果您更喜欢在Interface Builder中执行此操作而不编写代码:
答案 4 :(得分:1)
迅捷的方式:
它会更改导航中的所有项目。
在AppDelegate中执行以下操作:
let navControl = UINavigationBar.appearance()
navControl.tintColor = UIColor.grayColor()
答案 5 :(得分:0)
您当然可以更改后退按钮的颜色,也可以更改导航栏上的任何条形按钮。这是一个小片段。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
这只是其中一种方法。 我希望这有帮助!!我知道它的答案已被接受,但我想给它一个合适的片段。 干杯!!!快乐编码!!
答案 6 :(得分:0)
如果要将其设置为预定义样式,可以使用
[navigationBar setBarStyle: UIBarStyleBlack];
答案 7 :(得分:0)
将以下代码放在AppDelegate中,为全局后退按钮设置颜色
//Set color of BackButtonItem globally
[[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:5.0/255.0
green:127.0/255.0 blue:173.0/255.0 alpha:1.0]];
iOS 5及更高版本支持此功能
答案 8 :(得分:0)
它总是对我有用:
self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];
获取默认返回按钮
-
(UIBarButtonItem*) logicToAddBackButton
{
UIImageView *imageView;
// [imageView setTintColor:[UIColor redColor]];
UILabel *label=[[UILabel alloc] init];
if (WHITEBACK) {
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBackIPAD"]];
[label setTextColor:[UIColor whiteColor]];
}else{ //DEFAULTBACK
imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UiNavigationBack"]];
[label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];
}
[label setText:@"Back"];
[label sizeToFit];
int space=6;
label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space,
label.frame.origin.y, label.frame.size.width,
label.frame.size.height);
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space,
imageView.frame.size.height)];
view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width,
view.bounds.size.height);
UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(view.bounds.origin.x, view.bounds.origin.y,
view.bounds.size.width, view.bounds.size.height)];
button.bounds=CGRectMake(view.bounds.origin.x, view.bounds.origin.y, view.bounds.size.width,
view.bounds.size.height);
[button addTarget:self action:@selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:imageView];
[button addSubview:label];
[UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
CGRect orig=label.frame;
label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y -5, label.frame.size.width,
label.frame.size.height+10);
label.alpha = 1.0;
label.frame=orig;
} completion:nil];
UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:button];
return backButton;
}
返回按钮操作
(无效)eventBack { [self.navigationController popViewControllerAnimated:YES]; }
UiNavigationBack Image(请根据需要更改相同尺寸的图像颜色[25X41 144像素/英寸]
默认看)
答案 9 :(得分:0)
对我有用的是:
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];