如何更改UIBarButtonItem的颜色? [英] How to change the color of UIBarButtonItem?

查看:77
本文介绍了如何更改UIBarButtonItem的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIToolbar,然后将两个UIBarButtonItem添加到UIToolbar的项目中.如何更改UIBarButtomItem的颜色?我在文档中找不到API.

I have a UIToolbar, and then add two UIBarButtonItem to items of UIToolbar. How can I change the color of UIBarButtomItem? I did't find a API in the document.

推荐答案

请参阅更改UINavigationBarButtons的颜色"

see "Changing colors of UINavigationBarButtons"

由于域已关闭,因此我删除了链接...

I remove the link because the domain is down...

这是Google缓存中的文本:

The is the text from google cache:

好的,这是另一个快速提示.如何更改工具栏上按钮的颜色."当然,这可以应用于任何工具栏,但是我将在UINavigationBar上演示该过程.

Alright, here’s another quick tip. "How to change the colors of a button on a toolbar." Of course, this can be applied to any toolbar but I am going to demonstrate the procedure on a UINavigationBar.

上面的图像仅显示了几种颜色.实际上,您可以将按钮设置为所需的任何颜色.极好的!代码也很容易做到这一点.我们要做的第一件事是打开将要变为不同颜色的导航栏按钮的对象的头文件,并声明前向类UINavigationButton.您可以通过遍历UINavigationBar的子视图,读取其子视图的类名称或通过类转储UIKit(如果您已越狱的设备)来获取此类.

The above image only shows a couple of colors. In truth, you can make the button any color that you want. Fantastic! The code is really simple to do this as well. The first thing that we want to do is open the header file for whichever object will be turning a nav bar button a different color and declare the forward class UINavigationButton. You can get this class by either iterating through the subviews of the UINavigationBar, reading its subviews class names, or by class-dumping UIKit if you have a jailbroken device.

在接口声明之前放置以下行:

Place the following line before your interface declaration:

@class UINavigationButton;

现在,在标题中声明一个新方法,我们将使用该方法来实际更改按钮的颜色.

Now, declare a new method in the header that we will use to actually change the button’s color.

- (void)changeNavigationButtonColorToColor:(UIColor *)newColor

或者类似于上面的代码行.

Or something similar to the above line of code.

现在,打开对象的实施文件并实施上述方法.在文件中的任何位置,添加以下方法:

Now, open up your object’s implementation file and implement the above method. Anywhere in your file, add the following method:

- (void)changeNavigationButtonColorToColor:(UIColor *)newColor {
     for (UIView *view in self.navigationController.navigationBar.subviews) {
             NSLog(@"%@", [[view class] description]);
             if ([[[view class] description] isEqualToString:@"UINavigationButton"]) {
                     [(UINavigationButton *)view setTintColor:newColor];
             }
     }
   }

正如您在上面看到的那样,这实际上比起初看起来要容易得多.我们首先要做的是设置一个for循环,以使用NSFastEnumeration遍历UINavigationBar的子视图.然后,我们输出子视图的类名称,以供将来参考.如果类名是UINavigationButton,那么我们得到了视图.我们要做的就是设置UINavigationButton的tintColor属性.

As you can see above, this is actually a lot easier than it first appears to be. What we first do is set up a for loop to iterate through the subviews of the UINavigationBar using NSFastEnumeration. We then output the class name of the subview, for future reference. IF the class name is UINavigationButton, then we’ve got our view. All we do is set the tintColor property if the UINavigationButton.

就这样,我们完成了!

或者,如果您想要更大的范围,建议您创建一个新的UINavigationBar类别,然后在其中放置按钮颜色更改方法.这是您的方法可以由任何使用UINavigationBar的类执行,而不必一遍又一遍地重新创建相同的方法.

Alternatively, if you want a wider scope, I’d suggest creating a new UINavigationBar category and placing the button color changing method in there. This was your method can be performed by any class that uses a UINavigationBar without having to recreate the same method over and over.

请记住,后退按钮和导航按钮不是同一件事.您将不得不分别为后退按钮着色.

Remember, a back button and a navigation button are not the same thing. You will have to color the back button separately.

和往常一样,这是一个演示此代码的示例应用程序的链接: NavButtonColor.zip

And as usual, here’s a link to a sample app that demonstrates this code: NavButtonColor.zip

这篇关于如何更改UIBarButtonItem的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆