UINavigationController
UINavigationController继承于UIViewController(任何继承自UIViewController的类,都可以作为根控制器), 以栈的方式管理视图控制器.
UINavigationController的contentView始终显示栈顶的view
入栈,出栈
pushViewController:animated //进⼊入下⼀一个视图控制器
popViewControllerAnimated: //返回上⼀一个视图控制器
popToViewController:animated //返回到指定的视图控制器
popToRootViewControllerAnimated //返回到根视图控制器
常用属性
viewControllers //所有处于栈中的控制器
topViewController //位于栈顶的控制器
visibleViewController //当前正在显⽰示的控制器
navigationBar //导航条
-----------------------------
navigationcontroller直接控制viewcontrollers,navigationcontroller还包含navigationBar属性形成整个导航栏
navigationItem包含了bar视图的全部元素(如title,tileview,backBarButtonItem等),受当前viewcontroller管理, 每个navigationController页面的导航栏元素由所在页面的navigationItem管理。即设置当前页面的左右barbutton,用 self.navigationItem.leftBarButtonItem等
-----------------------------
//导航条的颜色
self.navigationController.navigationBar.barTintColor = [UIColor grayColor]; //导航栏的颜色
self.navigationController.navigationBar.tintColor = [UIColor blueColor]; //导航栏上的按钮颜色
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.hidden = NO; //隐藏导航栏,navigationBar的属性
self.navigationController.navigationBarHidden = YES; //也可以隐藏导航栏, 是navigationController的属性
//导航条文字属性
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:18], NSForegroundColorAttributeName:[UIColor redColor]};
self.navigationController.navigationBar.titleTextAttributes = dic;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1"] forBarMetrics:UIBarMetricsDefault];
////////////////
self.navigationItem.title = @"标题";
UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:@[@"郑州", @"新乡"]];
self.navigationItem.titleView = segmentC;
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:nil];
self.navigationItem.leftBarButtonItem = leftBtn;
//如果有多个btn,可以放在数组中,然后赋给barButtonItems
UIBarButtonItem *rightBtn1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(handleRightBtn:)];
UIBarButtonItem *rightBtn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(handleRightBtn:)];
UIBarButtonItem *rightBtn3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(handleRightBtn:)];
NSArray *arr = @[rightBtn1, rightBtn2, rightBtn3];
self.navigationItem.rightBarButtonItems = arr;
----------------------------------------------------------------------------------------------
UIToolBar
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.translucent = NO;
NSArray *arrBtns = @[btn1, btn2, btn3]; //创建三个UIBarButtonItem加入到数组中
self.toolbarItems = arrBtns;
此时按钮是挤在一起的, 解决方法
UIBarButtonItem *fixBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixBtn.width = 90;
添加在数组中btn1和btn2之间,btn2和btn3之间
方法二:
创建 UIBarButtonItem *fixBtn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
添加到数组中, 不需要设定宽度,系统自动设定。