iOS 导航栏 工具条
导航栏最常见的例子就是返回按钮的所在
在AppDelegate.m中,代码布局最开始定义窗口的时候, _window.rootViewController就应该为一个UINavigationController
这里的UINavigationController,戳进定义发现它是UIViewcontroller的子类
而之前代码布局中这里用的rootController是UIViewcontroller
所以它之中也是像之前代码布局中的UIViewcontroller一样是包含多个controller的,所以作为rootController
//按我自己理解,UINavigationController就是一个带导航栏的特殊UIViewcontroller,我们用导航栏,就选择用它
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc]init]]; _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; _window.rootViewController = rootNav; [_window makeKeyAndVisible]; return YES; }
导航栏暂时完成,下面去定义工具条
事实上NavigationController自带ToolBar,所以有更加方便的使用方法,使用自定义ToolBar可以是你有特殊需求,或者想自定样式(或者不知道自带ToolBar……)
先讲自定义的
我们可以新建一个View,父类选择UIToolBar,这就是我们自己的工具条View
ToolBar作为一个独立的View,和之前的代码布局中我们自己的Mainview没什么区别,想添加控件的方法都是一样的
只不过这个ToolBar只是一个工具栏,所以定位只在工具栏的一块小区域中定位,这是差不多唯一的不同
现在,我们要加载的有我们本身想要的一个View,通过改变rootController类型添加了导航栏,现在我们又要添加导航条,要加载的就不止一个View
在上次单纯的代码布局中,在Viewcontroller.m中重写的loadView是这样的
-(void)loadView { self.view = [[MainView alloc]initWithFrame:[UIScreen mainScreen].bounds]; }
直接把定义的根view赋值了我们自己的Mainview这一个,现在我们需要多个,就可以
-(void)loadView { [self.navigationItem setTitle:@"备忘录"]; [super loadView]; [self.view addSubview:[[NoteListView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]]; [self.view addSubview:[[NoteListToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width, 44)]]; }
先调用父类的loadView , [super loadView],来保证我们有一个基本的View存在,虽然他是空的,表示为黑屏
接着可以向这个View中使用addSubview添加你想要的View
另外这里的self是最开始的rootController的类型UINavigationController,使用它的属性来定义了标题
然后是使用自带的ToolBar
在Viewcontroller中,可以使用self.navigationController.toolbar找到这个自带的ToolBar
//顺便一提,关于self,在Appdelegate中,使用的是
// UINavigationController *rootNav = [[UINavigationController alloc]
initWithRootViewController:[[ViewController alloc]init]]; 来定义的window的rootcontroller
既然现在有了一个自带的ToolBar,我们只要把之前在我们自己的ToolBar文件中所添加的控件添加至这个自带的ToolBar即可
但是我们之前没发现它自带就是因为它默认是隐藏的……
在loadView中使用
[self.navigationController setToolbarHidden:NO];
来显示ToolBar
在loadView中,只要1、显示它 2、初始化它即可
可以在loadView函数下新增一个函数来专门放ToolBar的初始化
//toolbar @property(strong , nonatomic)UILabel *LBNotecount; @property(strong , nonatomic)UIButton *BTCreate; @end @implementation ViewController -(void)loadView { [self.navigationItem setTitle:@"导航栏"]; [super loadView]; _list =[[NoteListView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; [self.view addSubview:_list]; // _toolbar = [[NoteListToolbar alloc] // initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 44, [UIScreen mainScreen].bounds.size.width, 44)]; // [self.view addSubview: _toolbar]; // [_toolbar.BTCreate addTarget:self action:@selector(createPressed:)forControlEvents:UIControlEventTouchUpInside]; [self.navigationController setToolbarHidden:NO]; [self loadtoolbar:self.navigationController.toolbar]; //self.navigationController.toolbar; } -(void)loadtoolbar:(UIToolbar *)toolbar { _LBNotecount = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 60, toolbar.bounds.size.height/2 - 10, 120, 20)]; [_LBNotecount setText:@"工具栏"]; [_LBNotecount setTextAlignment:NSTextAlignmentCenter]; [toolbar addSubview:_LBNotecount]; _BTCreate = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; [_BTCreate setBackgroundColor:[UIColor greenColor]]; [toolbar addSubview:_BTCreate]; [_BTCreate addTarget:self action:@selector(createPressed:)forControlEvents:UIControlEventTouchUpInside]; }
SSH git@github.com:kakinuma4ko/iOSDmo.git