标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (UIBarButtonItem *)barButtonItemWithStyle:(UIBarButtonItemStyle)style; 5 - (void)buttonDidPush:(id)sender; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 self.title = @"UIBarButtonItem"; 14 UIImage *imgIcon = [UIImage imageNamed:@"Smile.png"]; 15 UIBarButtonItem *barBtnItemCustom = [[UIBarButtonItem alloc] initWithImage:imgIcon 16 style:UIBarButtonItemStylePlain 17 target:self 18 action:@selector(buttonDidPush:)]; 19 [self setToolbarItems:@[ 20 [self barButtonItemWithStyle:UIBarButtonItemStylePlain], 21 [self barButtonItemWithStyle:UIBarButtonItemStyleDone], 22 barBtnItemCustom 23 ] animated:YES]; 24 } 25 26 - (void)didReceiveMemoryWarning { 27 [super didReceiveMemoryWarning]; 28 // Dispose of any resources that can be recreated. 29 } 30 31 - (void)viewWillAppear:(BOOL)animated { 32 [self.navigationController setToolbarHidden:NO animated:NO]; 33 } 34 35 #pragma mark - Private Methods 36 - (UIBarButtonItem *)barButtonItemWithStyle:(UIBarButtonItemStyle)style { 37 NSString *title; 38 switch (style) { 39 case UIBarButtonItemStylePlain: 40 title = @"Plain正常"; 41 break; 42 case UIBarButtonItemStyleDone: 43 default: 44 title = @"Done加粗"; 45 break; 46 } 47 UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:title 48 style:style 49 target:nil 50 action:nil]; 51 return barBtnItem; 52 } 53 54 - (void)buttonDidPush:(id)sender { 55 if ([sender isKindOfClass:[UIBarButtonItem class]]) { 56 UIBarButtonItem *item = sender; 57 switch (item.style) { 58 case UIBarButtonItemStylePlain: 59 item.style = UIBarButtonItemStyleDone; 60 break; 61 case UIBarButtonItemStyleDone: 62 default: 63 item.style = UIBarButtonItemStylePlain; 64 break; 65 } 66 NSLog(@"item.style=%@", item.style == UIBarButtonItemStylePlain ? @"Plain" : @"Done"); 67 } 68 } 69 70 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 [_window addSubview:_navigationController.view]; 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4576079.html