//左边按钮
UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonLeft setFrame:CGRectMake(0, 0, 30, 40)];
[buttonLeft setTitle:@"变色" forState:0];
[buttonLeft setTitleColor:[UIColor whiteColor] forState:0];
[buttonLeft.titleLabel setFont:[UIFont boldSystemFontOfSize:13]];
[buttonLeft addTarget:self action:@selector(buttonClickleft) forControlEvents:UIControlEventTouchUpInside];
[buttonLeft setBackgroundColor:[UIColor blackColor]];
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:buttonLeft];
self.navigationItem.leftBarButtonItem = leftBarItem;
-(void)buttonClickleft{
UDNavigationController *nav = (UDNavigationController *)self.navigationController;
[nav setAlph];
}
//单独文件
import <UIKit/UIKit.h>
@interface UDNavigationController : UINavigationController{
BOOL _changing;
}
@property(nonatomic, retain)UIView *alphaView;
-(void)setAlph;
@end
import "UDNavigationController.h"
@implementation UDNavigationController
@synthesize alphaView;
-(id)initWithRootViewController:(UIViewController *)rootViewController{
self = [super initWithRootViewController:rootViewController];
if (self) {
CGRect frame = self.navigationBar.frame;
alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height+20)];
alphaView.backgroundColor = [UIColor blueColor];
[self.view insertSubview:alphaView belowSubview:self.navigationBar];
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"bigShadow.png"] forBarMetrics:UIBarMetricsCompact];
self.navigationBar.layer.masksToBounds = YES;
}
return self;
}
-(void)setAlph{
if (_changing == NO) {
_changing = YES;
if (alphaView.alpha == 0.0 ) {
[UIView animateWithDuration:0.5 animations:^{
alphaView.alpha = 1.0;
} completion:^(BOOL finished) {
_changing = NO;
}];
}else{
[UIView animateWithDuration:0.5 animations:^{
alphaView.alpha = 0.0;
} completion:^(BOOL finished) {
_changing = NO;
}];
}
}
}
@end