开发者

Change UIBarButtonItem color

I have created a custom bar button item in code.

UIButton* backButton = [UIButton buttonWithType:102]; // left-pointing shape!
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Custom Item" forState:UI开发者_如何学JAVAControlStateNormal];

// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backItem;

But it displays as dark blue. I need to get the default look and feel for this. How to set the default look and feel?

Regards,

Dilshan


Probably the wrong question, but if you are concerned about look and feel why are you using undefined UIButtonType to finesse 'left-pointing' button? The left-pointing button is intended for popping back up navigation stack, and I think that it pretty much comes for free if you use UINavigationController.


see "Changing colors of UINavigationBarButtons"

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

The is the text from google cache:


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.

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

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.

That’s it, we’re done!

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.

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


@Rob: yes, it works for the toolbar, too!

- (void)changeToolbarButtonColorToColor:(UIColor *)newColor {
    for (UIView *view in self.navigationController.toolbar.subviews) {                 
        if ([[[view class] description] isEqualToString:@"UIToolbarButton"]) {                        
            [(UINavigationButton *)view setTintColor:newColor];            
        }   
    }    
}

Be carefully: You have to use "toolbar" in the 2nd line and "UIToolbarButton" in the 3rd line.
Add this code in your viewDidLoad:

- (void)viewDidLoad {   
    [self changeToolbarButtonColorToColor: [UIColor colorWithRed:0.0/255 green:163.0/255 blue:224.0/255 alpha:1]];
}

That's all! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜