xcode iPhone 设置后栏按钮项的色调颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11583603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone Set Tint Color of Back Bar Button Item
提问by user717452
I am trying to set the tint color of the back button within a navigation controller, but nothing is working. I have tried
我正在尝试在导航控制器中设置后退按钮的色调,但没有任何效果。我试过了
[self.navigationController.backBarButtonItem setTintColor:myColor];
//myColor was previously set
But it stays the default tint
但它保持默认色调
回答by Qiming
I believe barButtonItem is read-only. (I'm not too sure about this, someone correct me if I'm wrong)
我相信 barButtonItem 是只读的。(我对此不太确定,如果我错了,有人纠正我)
To give a tint colour to these buttons, this is what I would do:
为了给这些按钮着色,这就是我要做的:
In your App Delegate, add these lines of code:
在您的 App Delegate 中,添加以下代码行:
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // Change to your colour
The first line sets an appearance proxy, so I believe this works too, if you like less code:
第一行设置了一个外观代理,所以我相信这也有效,如果你喜欢更少的代码:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
I hope this works for you! (This changes all instances of UIBarButtonItem)
我希望这对你有用!(这会更改 UIBarButtonItem 的所有实例)
回答by Deepak Singh Negi
You can't change the tint color of UINavigationItem directly. You have to change the tint color of navigation bar and your bar button will automatically change its color.
您不能直接更改 UINavigationItem 的色调颜色。您必须更改导航栏的色调颜色,您的栏按钮将自动更改其颜色。
Add these lines in your viewWillAppear method
在您的 viewWillAppear 方法中添加这些行
[[self.navigationController navigationBar] tintColor] = [UIColor myColor];
Or You can create a custom button and initialize your UIBarButtonItem as
或者您可以创建一个自定义按钮并将您的 UIBarButtonItem 初始化为
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourUIButton];
回答by Eric
This worked for me.
这对我有用。
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
It is very similar to the second answer...
它与第二个答案非常相似......
回答by Alex
If you prefer to do it in Interface Builder without writing code:
如果您更喜欢在 Interface Builder 中执行此操作而无需编写代码:
- Select your UINavigationBar (inside UINavigationController)
- 选择您的 UINavigationBar(在 UINavigationController 内)
- In Attributes Inspector, find "Tint" - this is what sets Back Button color. Note that "Bar Tint" is not the same - this sets Navigation Bar background color.
- 在属性检查器中,找到“Tint” - 这是设置后退按钮颜色的原因。请注意,“Bar Tint”不一样 - 这会设置导航栏背景颜色。
It is important to set Back Button appearance this way, and not try to implement leftBarButtonItem, because implementing leftBarButtonItem will disable the built-in back navigation gestures, like swipe to go back.
以这种方式设置后退按钮外观很重要,而不是尝试实现 leftBarButtonItem,因为实现 leftBarButtonItem 将禁用内置的后退导航手势,如滑动返回。
回答by Joseph Geraghty
Swift way:
快捷方式:
It changes all items in the nav.
它会更改导航中的所有项目。
In AppDelegate do something like this:
在 AppDelegate 中做这样的事情:
let navControl = UINavigationBar.appearance()
navControl.tintColor = UIColor.grayColor()
回答by Yogesh Lolusare
It always work for me:
它总是对我有用:
self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];
GET DEFAULT BACK BUTTON
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;
}
BACK BUTTON ACTION
(void)eventBack { [self.navigationController popViewControllerAnimated:YES]; }
UiNavigationBack Image (Please change colour of image as require with same size [25X41 144pixels/inch] to get default look)
返回按钮操作
(void)eventBack { [self.navigationController popViewControllerAnimated:YES]; }
UiNavigationBack 图片(请根据需要更改图片颜色,大小相同 [25X41 144pixels/inch] 以获得默认外观)
回答by Nilesh Agrawal
What worked for me was this :
对我有用的是:
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor textPrimaryColor] forKey:NSForegroundColorAttributeName];
回答by Apple_iOS0304
Well you can certainly change the color of just the Back Button or perhaps any Bar Button on the Navigation bar. Here's a small snippet to do it.
好吧,您当然可以仅更改后退按钮或导航栏上的任何栏按钮的颜色。这是一个小片段来做到这一点。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil];
backButton.tintColor = [UIColor blackColor];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];
Well this is just one of the ways for doing this. I hope this helps!! I know the answer for it is already accepted, but I thought to give a proper snippet to it. Cheers!!! Happy Coding!!
嗯,这只是这样做的方法之一。我希望这有帮助!!我知道它的答案已经被接受,但我想给它一个适当的片段。干杯!!!快乐编码!!
回答by r0ro
If you want to set it to predefined style you can use
如果要将其设置为预定义样式,可以使用
[navigationBar setBarStyle: UIBarStyleBlack];
回答by Jayprakash Dubey
Place the following code in AppDelegate that will set color for Back button globally
将以下代码放在 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]];
This is supported in iOS 5 and above
这在 iOS 5 及更高版本中受支持