ios 自定义导航栏,开启侧滑返回手势
2015-12-16 10:32
555 查看
自定义一个常用ListViewController
.h文件
.m文件
自定义GuestomViewController,
.m
完成~
.h文件
#import <UIKit/UIKit.h> @interface ListViewController : UIViewController -(void)diquButtonClick; @end
.m文件
// // ListViewController.m // OuLianWang // // Created by allenariel on 15/6/24. // Copyright (c) 2015年 皇室美家. All rights reserved. // #import "ListViewController.h" #import "ZDYTabBarController.h" #import "GustomViewController.h" @interface ListViewController () @end @implementation ListViewController -(void)loadView { [super loadView]; [self deplyNavigationController]; self.view.backgroundColor=XZColor(238, 238, 238); } - (void)viewDidLoad { [super viewDidLoad]; [self deplyNavigationController]; // [self hideTabBar]; } -(void)viewWillAppear:(BOOL)animated { ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController; tabBar.zqTabBar.hidden=YES; } -(void)hideTabBar { ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController; tabBar.zqTabBar.hidden=YES; // self.view.backgroundColor=ZQColor(232, 232, 232); } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController; tabBar.zqTabBar.hidden=NO; } #pragma mark-------配置导航栏 -(void)deplyNavigationController { GustomViewController *navi = [GustomViewController new]; self.navigationController.navigationBar.barTintColor=XZColor(22,173,249); [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15], NSForegroundColorAttributeName:[UIColor whiteColor]}]; UIButton *diqubutton=[UIButton buttonWithType:UIButtonTypeCustom]; [diqubutton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [diqubutton addTarget:self action:@selector(diquButtonClick) forControlEvents:UIControlEventTouchUpInside]; diqubutton.titleLabel.font=[UIFont boldSystemFontOfSize:18]; diqubutton.frame=CGRectMake(0, 0,20,20); UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton]; // 调整 leftBarButtonItem 在 iOS7 下面的位置 if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)) { UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -10;//这个数值可以根据情况自由变化 self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton]; } else self.navigationItem.leftBarButtonItem = leftbutton; self.tabBarController.tabBar.hidden=YES; } -(void)diquButtonClick { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
自定义GuestomViewController,
.m
// // GustomViewController.m // 暇课 // // Created by mac on 15/12/16. // Copyright © 2015年 暇课. All rights reserved. // #import "GustomViewController.h" @interface GustomViewController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate> @end @implementation GustomViewController - (instancetype)initWithRootViewController:(UIViewController *)rootViewController { if (self = [super initWithRootViewController:rootViewController]) { // 设置navigationBar的背景颜色,根据需要自己设置 self.navigationBar.barTintColor = XZColor(22,173,249); // 设置navigationBar是否透明,不透明的话会使可用界面原点下移(0,0)点为导航栏左下角下方的那个点 self.navigationBar.translucent = NO; // 设置navigationBar是不是使用系统默认返回,默认为YES self.interactivePopGestureRecognizer.enabled = YES; // 创建一个颜色,便于之后设置颜色使用 UIColor * color = [UIColor whiteColor]; // 设置navigationBar元素的背景颜色,不包括title self.navigationBar.tintColor = color; // 设置navigationController的title的字体颜色 NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName]; self.navigationBar.titleTextAttributes = dict; self.navigationBar.barTintColor=XZColor(22,173,249); [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15], NSForegroundColorAttributeName:[UIColor whiteColor]}]; UIButton *diqubutton=[UIButton buttonWithType:UIButtonTypeCustom]; [diqubutton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [diqubutton addTarget:self action:@selector(diquButtonClick) forControlEvents:UIControlEventTouchUpInside]; diqubutton.titleLabel.font=[UIFont boldSystemFontOfSize:18]; diqubutton.frame=CGRectMake(0, 0,20,20); UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton]; // 调整 leftBarButtonItem 在 iOS7 下面的位置 if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)) { UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -10;//这个数值可以根据情况自由变化 self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton]; } else self.navigationItem.leftBarButtonItem = leftbutton; self.tabBarController.tabBar.hidden=YES; } return self; } //- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated //{ // // fix 'nested pop animation can result in corrupted navigation bar' // if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { // self.interactivePopGestureRecognizer.enabled = NO; // } // // [super pushViewController:viewController animated:animated]; //} // //- (void)navigationController:(UINavigationController *)navigationController // didShowViewController:(UIViewController *)viewController // animated:(BOOL)animated //{ // if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { // navigationController.interactivePopGestureRecognizer.enabled = YES; // } //} - (void)viewDidLoad { [super viewDidLoad]; // 为self创建弱引用对象 __weak typeof (self) weakSelf = self; if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.interactivePopGestureRecognizer.delegate = weakSelf; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
完成~
相关文章推荐
- iOS 开发之 runtime运行时模拟成员变量
- ios集成语音朗读功能
- ios addSubview和insertSubview的区别
- ios nsstring 字符串包含引号 处理方法
- iOS10 NSBundle
- iOS之 开发常用到的宏定义
- iOS09-NSUserDefault
- iOS08 页面跳转动画设置方法
- iOS CoreData数据迁移
- iOS05 PropertyList,即属性列表文件
- 关于tableviewHeader盖住cell的问题解决
- ios开发——点击空白处隐藏键盘的几种方法
- iOS开发之XML解析
- Nagios安装配置详解
- IOS 开发调试方法
- iOS01 计算器
- iOS开发常用的RGB色值
- c#上iOS apns p12文件制作记录
- IOS多线程系统学习之NSThread
- iOS-收集的不常用却实用的小方法和技巧