我正在尝试在导航控制器中设置后退按钮的色调,但没有任何效果。我努力了
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
但它保持默认色调
我正在尝试在导航控制器中设置后退按钮的色调,但没有任何效果。我努力了
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
但它保持默认色调
我相信 barButtonItem 是只读的。(我不太清楚,如果我错了,请有人纠正我)
为了给这些按钮着色,我会这样做:
在您的 App Delegate 中,添加以下代码行:
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour
第一行设置了一个外观代理,所以我相信这也可以,如果你喜欢更少的代码:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
我希望这对你有用!(这会改变 UIBarButtonItem 的所有实例)
您不能直接更改 UINavigationItem 的色调颜色。您必须更改导航栏的色调颜色,您的栏按钮将自动更改其颜色。
在您的 viewWillAppear 方法中添加这些行
[[self.navigationController navigationBar] tintColor] = [UIColor myColor];
或者您可以创建一个自定义按钮并将您的 UIBarButtonItem 初始化为
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
这对我有用。
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
这与第二个答案非常相似......
快捷方式:
它会更改导航中的所有项目。
在 AppDelegate 中执行以下操作:
let navControl = UINavigationBar.appearance()
navControl.tintColor = UIColor.grayColor()
它总是对我有用:
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;
}
返回按钮操作
(void)eventBack { [self.navigationController popViewControllerAnimated:YES]; }
UiNavigationBack Image(请根据需要更改图像的颜色,尺寸相同 [25X41 144pixels/inch] 以获得默认外观)
好吧,您当然可以仅更改后退按钮或导航栏上的任何条形按钮的颜色。这是一个小片段。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
好吧,这只是其中一种方法。我希望这有帮助!!我知道它的答案已经被接受,但我想给它一个适当的片段。干杯!!!编码快乐!!
将以下代码放在 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 及更高版本中受支持
如果要将其设置为预定义样式,可以使用
[navigationBar setBarStyle: UIBarStyleBlack];
对我有用的是:
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];