开发中自己常用的关于导航栏的编辑设置,都是经过IOS 8下验证过的,汇总如下,便于开发中查阅。
1、隐藏默认的返回按钮导航栏:
self.navigationItem.hidesBackButton = YES;
2、自定义默认返回导航标题:
- (void) setLeftNavigationItem
{
UIBarButtonItem *returnItem = [[UIBarButtonItem alloc] init];
returnItem.title = @"返回";
self.navigationItem.backBarButtonItem = returnItem;
}
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
3、设置(左)右导航栏
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] init];
rightItem.customView = self.headNavigationImageView;
rightItem.customView = self.headNavigationImageView;
//图片设置为圆形
self.headNavigationImageView.frame = CGRectMake(15, 15, 40, 40);
self.headNavigationImageView.layer.cornerRadius = 20;
self.headNavigationImageView.clipsToBounds = YES;
self.headNavigationImageView.frame = CGRectMake(15, 15, 40, 40);
self.headNavigationImageView.layer.cornerRadius = 20;
self.headNavigationImageView.clipsToBounds = YES;
self.navigationItem.rightBarButtonItem = rightItem;
4、设置导航栏和状态栏的背景色:
//设置状态栏的背景色
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0];
[self.navigationController.navigationBar addSubview:statusBarView];
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 20)];
statusBarView.backgroundColor=[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0];
[self.navigationController.navigationBar addSubview:statusBarView];
//设置导航栏的背景色
[self.navigationController.navigationBar setBackgroundColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
//设置导航栏和状态栏背景颜色(统一设置)
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:20/255.0 green:155/255.0 blue:213/255.0 alpha:1.0]];
5、在导航栏中设置自定义标题
UILabel *wTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0,[[UIScreen mainScreen] bounds].size.width - 160, _navigationheight)];
// wTitleLabel.backgroundColor = [UIColor blackColor];
wTitleLabel.font = [UIFont systemFontOfSize:22];
wTitleLabel.textColor = [UIColor whiteColor];
wTitleLabel.backgroundColor = [UIColor clearColor];
wTitleLabel.textAlignment = NSTextAlignmentCenter;
wTitleLabel.text = @"设置";
self.navigationItem.titleView = wTitleLabel;
self.navigationItem.titleView.frame = CGRectMake(15, 15, 40, 40);
// wTitleLabel.backgroundColor = [UIColor blackColor];
wTitleLabel.font = [UIFont systemFontOfSize:22];
wTitleLabel.textColor = [UIColor whiteColor];
wTitleLabel.backgroundColor = [UIColor clearColor];
wTitleLabel.textAlignment = NSTextAlignmentCenter;
wTitleLabel.text = @"设置";
self.navigationItem.titleView = wTitleLabel;
self.navigationItem.titleView.frame = CGRectMake(15, 15, 40, 40);
6、手动设定rootVC
DefaultViewController *dVC = [[DefaultViewController alloc] initWithNibName:@"DefaultViewController " bundle:nil];
dVC.title = @" ";
dVC.navigationController.navigationBarHidden = NO;
UINavigationController * navigationCtr = [[UINavigationController alloc] initWithRootViewController:dVC];
[[UIApplication sharedApplication].delegate window].rootViewController = navigationCtr;
dVC.title = @" ";
dVC.navigationController.navigationBarHidden = NO;
UINavigationController * navigationCtr = [[UINavigationController alloc] initWithRootViewController:dVC];
[[UIApplication sharedApplication].delegate window].rootViewController = navigationCtr;
7、未完待续。
参考资料: