标签:
效果如下:
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 - (void)nextDidPush; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 NSLog(@"ViewController viewDidLoad"); 11 [super viewDidLoad]; 12 13 //注意如果是作为父类有继承关系的话,不能封装执行代码为方法;因为加载子类时不会再进一步执行封装的方法 14 //[self layoutUI]; 15 self.view.backgroundColor = [UIColor whiteColor]; 16 UIButton *btnNext = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 17 btnNext.frame = CGRectMake(0, 0, 150, 40); 18 CGPoint newPoint = self.view.center; 19 newPoint.y += 10; 20 btnNext.center = newPoint; 21 btnNext.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 22 btnNext.layer.masksToBounds = YES; 23 btnNext.layer.cornerRadius = 10.0; 24 btnNext.layer.borderColor = [UIColor colorWithRed:0.275f green:0.641f blue:0.840f alpha:1.00f].CGColor; 25 btnNext.layer.borderWidth = 2.0; 26 [btnNext setTitle:@"下一画面" forState:UIControlStateNormal]; 27 [btnNext addTarget:self action:@selector(nextDidPush) forControlEvents:UIControlEventTouchUpInside]; 28 [self.view addSubview:btnNext]; 29 } 30 31 - (void)didReceiveMemoryWarning { 32 [super didReceiveMemoryWarning]; 33 // Dispose of any resources that can be recreated. 34 } 35 36 - (void)nextDidPush { 37 ViewController *nextViewController = [[ViewController alloc] init]; 38 //定义为静态变量,属于类变量;保证多次创建对象实例并调用此方法时,下面if语句只执行一次 39 static BOOL isFirst = YES; 40 if (isFirst) { 41 nextViewController.hidesBottomBarWhenPushed = YES; 42 isFirst = NO; 43 } 44 [self.navigationController pushViewController:nextViewController animated:YES]; 45 } 46 47 @end
CustomToolbar.h
1 #import <UIKit/UIKit.h> 2 3 #import "ViewController.h" 4 @interface CustomToolbar : ViewController 5 @end
CustomToolbar.m
1 #import "CustomToolbar.h" 2 3 @interface CustomToolbar () 4 - (void)layoutUI; 5 - (void)buttonDidPush:(UIBarButtonItem *)sender; 6 @end 7 8 @implementation CustomToolbar 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)viewWillAppear:(BOOL)animated { 22 [super viewWillAppear:animated]; 23 [self.navigationController setNavigationBarHidden:NO animated:YES]; 24 [self.navigationController setToolbarHidden:NO animated:YES]; 25 } 26 27 - (void)layoutUI { 28 self.title = @"CustomToolbar"; 29 //工具条左侧按钮 30 UIBarButtonItem *barBtnItemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(buttonDidPush:)]; 31 barBtnItemLeft.tag = 1; 32 33 //工具条中间自动伸缩空白填充按钮 34 UIBarButtonItem *barBtnItemSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 35 //工具条右侧按钮 36 UIBarButtonItem *barBtnItemRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(buttonDidPush:)]; 37 barBtnItemRight.tag = 2; 38 39 [self setToolbarItems:@[barBtnItemLeft, barBtnItemSpacer, barBtnItemRight] animated:YES]; 40 } 41 42 - (void)buttonDidPush:(UIBarButtonItem *)sender { 43 UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"标题信息" 44 message:sender.tag == 1 ? @"工具条左侧按钮触发" : @"工具条右侧按钮触发" 45 delegate:nil 46 cancelButtonTitle:nil 47 otherButtonTitles:@"确定", nil]; 48 [alertV show]; 49 } 50 51 @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 #import "CustomToolbar.h" 4 5 @interface AppDelegate () 6 @end 7 8 @implementation AppDelegate 9 10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 11 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 12 CustomToolbar *customToolbar = [[CustomToolbar alloc] init]; 13 _navigationController = [[UINavigationController alloc] initWithRootViewController:customToolbar]; 14 _window.rootViewController = _navigationController; 15 [_window addSubview:_navigationController.view]; 16 [_window makeKeyAndVisible]; 17 return YES; 18 } 19 20 - (void)applicationWillResignActive:(UIApplication *)application { 21 } 22 23 - (void)applicationDidEnterBackground:(UIApplication *)application { 24 } 25 26 - (void)applicationWillEnterForeground:(UIApplication *)application { 27 } 28 29 - (void)applicationDidBecomeActive:(UIApplication *)application { 30 } 31 32 - (void)applicationWillTerminate:(UIApplication *)application { 33 } 34 35 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4576073.html