1.设置nav图片
//设置图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"]forBarMetrics:UIBarMetricsDefault];
//去除导航栏下面的黑线
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
2.设置透明导航栏
要遵循UINavigationControllerDelegate,UIGestureRecognizerDelegate两个代理。
设置代理
self.navigationController.delegate = self;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.edgesForExtendedLayout = UIRectEdgeNone;
代理方法:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animate{
if([viewController isKindOfClass:[self class]]){
[navigationController setNavigationBarHidden:YES animated:YES];
}
else{
[navigationController setNavigationBarHidden:NO animated:YES];
}
}
以上,就可以设置透明导航栏了。
3.在公共父类控制器中设置navtitle
首先在父类控制器的.h文件中声明类型为NSString的title
@property(nonatomic ,copy)NSString *navTitle;
然后是设置title代码在.m文件中
#pragma mark -- 设置title
- (void)setNavTitle:(NSString *)navTitle {
_navTitle = navTitle;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
UILabel *titleLabel = [UILabel new];
titleLabel.frame = titleView.bounds;
titleLabel.font = [UIFont systemFontOfSize:18.f];
titleLabel.text = navTitle;
titleLabel.textColor = RGB(51,51,51);
titleLabel.textAlignment = NSTextAlignmentCenter;
[titleView addSubview:titleLabel];
self.navigationItem.titleView = titleView;
}