UINavigationController相关
创建导航栏控制器,并添加到主窗口中
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:[[ZFViewControllerFirst alloc]init]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
@end
当一个ViewController对象添加到导航栏控制器中,ViewController对象的navigationController就已经确定,在ViewController对象中就可以对navigationController进行操作
//跳转下一个ViewController,加载新的ViewController采用的是压栈的方式
[self.navigationController pushViewController:[[OtherViewController alloc]init] animated:YES];
//弹出当前ViewController返回上一个ViewController
[self.navigationController popViewControllerAnimated:YES];
//返回指定的某一级ViewController
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
//返回最初始的ViewController,根ViewController
[self.navigationController popToRootViewControllerAnimated:YES];
如要修改当前ViewController的navigationController的标题
[self.navigationItem setTitle:@"第三屏"];
修改当前ViewController的navigationController左右键
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"左导航键" style:UIBarButtonItemStylePlain target:self action:@selector(leftBtnClick)];
[self.navigationItem setLeftBarButtonItem:leftBarBtn];
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"右导航键" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarClick)];
[self.navigationItem setRightBarButtonItem:rightBarBtn];
}
- (void)leftBtnClick
{
NSLog(@"%s",__func__);
}
- (void)rightBarClick
{
NSLog(@"%s",__func__);
}
UIBarButtonItem的初始化方式
//以XIB的方式初始化
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//以图片的方式初始化
- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
- (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action NS_AVAILABLE_IOS(5_0); // landscapeImagePhone will be used for the bar button image when the bar has Compact or Condensed bar metrics.
//以标题文字的方式初始化
- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;
//以BarButtonSystemItem系统控件进行初始化
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;
//以自定义View初始化
- (instancetype)initWithCustomView:(UIView *)customView;
隐藏导航条背景和阴影
直接设置导航条透明是无效的,但可以传一个空的图片作为导航条背景,这样看起来导航条就是空的,阴影同理
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
/*
forBarMetrics:
UIBarMetricsDefault,
UIBarMetricsCompact,
UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar
UIBarMetricsCompactPrompt,
*/
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
导航条背景和阴影透明度
为UIImage定义一个分类categray,功能为将颜色转为图片
#import "UIImage+Image.h"
@implementation UIImage (Image)
+ (UIImage *)imageWithColor:(UIColor *)color
{
// 描述矩形
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
// 开启位图上下文
UIGraphicsBeginImageContext(rect.size);
// 获取位图上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 使用color演示填充上下文
CGContextSetFillColorWithColor(context, [color CGColor]);
// 渲染上下文
CGContextFillRect(context, rect);
// 从上下文中获取图片
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
return theImage;
}
@end
用这个类可以将指定的颜色转换成图片
//设置一个透明度的颜色
UIColor *alphaColor = [UIColor colorWithWhite:1 alpha:0.5];
//转换颜色为图片
UIImage *alphaImage = [UIImage imageWithColor:alphaColor];
[self.navigationController.navigationBar
//将这个图片作为导航条的背景
setBackgroundImage:alphaImage forBarMetrics:UIBarMetricsDefault];
修改导航条标签文字的颜色
NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
//或者这样写
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:22];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
[self.navigationController.navigationBar setTitleTextAttributes:dict];
使用NavigationBar Appearance整体修改导航条样式
获取全局导航条标识UINavigationBar appearance。appearance是个协议,只要遵守这个协议都会有这个方法
UINavigationBar *navigationBar = [UINavigationBar appearance];
[navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:22];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
[navigationBar setTitleTextAttributes:dict];
如果只希望个别几个控制器的导航条风格修改为统一样式
#import "ZFCustomNavigationController.h"
#import "ZFArenaViewController.h"
@interface ZFCustomNavigationController ()
@end
@implementation ZFCustomNavigationController
+ (void)initialize
{
NSLog(@"%s",__func__);
//设置为类方法所在类遵守appearance协议,
//这样使用自定义的NavigationController为自定义风格
//不使用该自定义NavigationController的为默认或其他风格
NSArray *appearanceArr = @[[ZFCustomNavigationController class]];
UINavigationBar *navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:appearanceArr];
[navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:22];
dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
[navigationBar setTitleTextAttributes:dict];
}
统一修改返回按钮,比如非根以上控制器设置自定义的返回按钮,但要依然保持左滑动手势可以返回上一级控制器
//先实现UINavigationControllerDelegate代理
@interface ZFCustomNavigationController ()<UINavigationControllerDelegate>
@property(nonatomic,strong)id popGestureDelegate;
@end
@implementation ZFCustomNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.popGestureDelegate = self.interactivePopGestureRecognizer.delegate;
self.delegate = self;
}
//如果只是一味的将手势代理清空,在根控制器使用手势依然有效,但是根控制器是不能被移除的,
//因此,在只剩根控制器还需要还原代理,这样就需要实现UINavigationControllerDelegate代理中的方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if(self.viewControllers[0] == viewController)
{
self.interactivePopGestureRecognizer.delegate = self.popGestureDelegate;
}
else
{
//想统一设置返回按钮,滑动移除控制器
//清除手势代理就能实现滑动返回,iOS6不支持
self.interactivePopGestureRecognizer.delegate = nil;
}
}
//设置统一的返回按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
if(self.viewControllers.count > 1)
{
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithNoRenderImageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
}
}
@end
跳转到非根控制器隐藏tabbar
setting.hidesBottomBarWhenPushed = NO;