在Appledelegate.m中将导航控制器设置为根视图控制器,这样APP运行就直接进入导航控制器
//导航控制器作为根视图控制器
//导航控制器 管理有层次递进关系的视图控制器
RootViewController *rootVC = [[RootViewController alloc] init];
//rootVC作为导航控制器的根视图控制器
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];
//导航控制器作为window的根视图控制器
self.window.rootViewController = navi;
导航栏的定制
//1.对导航栏自身属性的设置,因为导航栏是唯一的,所以对导航栏自身的设置会影响到每一个页面
{
//导航栏的半透明效果,会影响self.view子视图的坐标系
//半透明效果打开,子视图的坐标原点是屏幕的左上角
//半透明效果关闭,子视图的坐标原点是导航栏的左下角
//导航栏高度44, 状态栏高度20 ,导航栏的半透明效果会延伸到状态栏,所以半透明效果的高度是64
self.navigationController.navigationBar.translucent = YES;
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 64, 100, 50)];
// view.backgroundColor = [UIColor redColor];
//
// [self.view addSubview:view];
//导航栏的背景颜色
// self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
//背景图片
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2233"] forBarMetrics:UIBarMetricsDefault];
//导航栏隐藏掉.半透明属性关掉时,子视图的坐标系原点是屏幕的左上角
// self.navigationController.navigationBar.hidden = YES;
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
// view.backgroundColor = [UIColor whiteColor];
// [self.view addSubview:view];
}
//2.对self.navigationItem的设置,只影响当前页面
// self.navigationItem
{
//导航栏标题
// self.title = @"第一页";
self.navigationItem.title = @"设置";
//自定义titleView
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]];
// seg.backgroundColor = [UIColor purpleColor];
[seg addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = seg;
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(leftBarButtonItemAction:)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
// self.navigationItem.leftBarButtonItems = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftBarButtonItemAction:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ] initWithImage:[UIImage imageNamed:@"iconfont-daxiao"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
//导航栏上视图渲染颜色
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
**导航栏透明问题可以参考文章:导航栏透明度问题 http://www.jianshu.com/p/96fb78e70740
iOS 导航栏透明,变色动画 http://www.jianshu.com/p/c4b542913e6f **
回调方法
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar{
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:arc4random() % 256 / 255.0];
}
- (void)segmentedControlAction:(UISegmentedControl *)seg{
NSLog(@"nihao");
}
网友评论