UINavigationBar,UIBarButtonItem 相关

1. Adding a Custom UIBarButtonItem
We will use an image in the resources folder called “back.png”. To insert the image as the back button, add this piece of code to the AppDelegate.m file. This should be in the application:didFinishLaunchingWihOptions method.
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIImage *navBarImage = [UIImage imageNamed:@"menubar.png"];

     [[UINavigationBar appearance] setBackgroundImage:navBarImage
                                       forBarMetrics:UIBarMetricsDefault];
    UIImage* backButtonImage = [UIImage imageNamed:@"back.png"];

     [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

     return YES;
}


2. A Simple Way to Animate a UIBarButtonItem
My first pass solution to animate a UIBarButtonItem was to initialize it's custom view with a UIImageView. The UIImageView can then be set up for animation with multiple images. Calling startAnimaiton/stopAnimation on the UIImaveView will then animate. This is great for animating buttons in tool and tab bars.

One problem is that the when initializing the UIImageView into the UIBarButtonItem, the UIBarButtonItem does not respond to touch events. Yuk.

A simple solution is to initialize the UIBarButtonItem custom view with a UIButton, then touch events for the UIBarButtonItem item are handle correctly.

But the UIButton does not have a way to use a UIImageView only UIImages. We want to use the UIImageView for its simple animation.

The solution is to add the UIImageView as a subview of the UIButton.

Here is the code.
NSArray *images = [NSArray arrayWithObjects:
        [UIImage imageNamed:@"image0.png"],
        [UIImage imageNamed:@"image1.png"],
        nil];
imageView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"image0.png"]];
imageView.animationImages = images;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = self.imageView.bounds;
[button addSubview:self.imageView];
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];

Some things to notice:
The UIButton is of zero area as it does not have its bounds set upon initialization, thus the bounds are initialized with the bounds of the UIImageView (which has its bounds initialized from the image).

The UIButton handles the action/target for the touch event. The UIBarButtonItem's action/target are not set.


To animate:
    [imageView startAnimating];

Have fun,
Sean

===========================================
As you know, until iOS 5 came out, we used drawRect override in AppDelegate to customize UINavigationBar. But know, iOS 5 give us some new method for styling (and old doesn’t work).

How to build app that will work on iOS 4 and iOS 5 with stylized UINavigationBar?

You must to do both!

In AppDelegate use this code:
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:@"navbar.png"];
[img drawInRect:rect];
}
@end


and in viewDidLoad method for iOS5 (in your view implementation):
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
}


If you see, here we are asking if navbar will respondToSelector to avoid crash on iOS4!



你可能感兴趣的:(uibarbuttonitem)