码迷,mamicode.com
首页 > 移动开发 > 详细

ios 自定义导航栏,开启侧滑返回手势

时间:2015-12-16 12:11:12      阅读:1393      评论:0      收藏:1      [点我收藏+]

标签:

自定义一个常用ListViewController

.h文件

1 #import <UIKit/UIKit.h>
2 
3 @interface ListViewController : UIViewController
4 
5 -(void)diquButtonClick;
6 
7 @end

.m文件

  1 //
  2 //  ListViewController.m
  3 //  OuLianWang
  4 //
  5 //  Created by allenariel on 15/6/24.
  6 //  Copyright (c) 2015年 皇室美家. All rights reserved.
  7 //
  8 
  9 #import "ListViewController.h"
 10 #import "ZDYTabBarController.h"
 11 #import "GustomViewController.h"
 12 
 13 @interface ListViewController ()
 14 
 15 @end
 16 
 17 @implementation ListViewController
 18 -(void)loadView
 19 {
 20      [super loadView];
 21      [self deplyNavigationController];
 22      self.view.backgroundColor=XZColor(238, 238, 238);
 23 }
 24 - (void)viewDidLoad
 25 {
 26     
 27     [super viewDidLoad];
 28     [self deplyNavigationController];
 29 
 30 //    [self hideTabBar];
 31     
 32 }
 33 -(void)viewWillAppear:(BOOL)animated
 34 {
 35     
 36     ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController;
 37     tabBar.zqTabBar.hidden=YES;
 38    
 39 }
 40 -(void)hideTabBar
 41 {
 42     ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController;
 43     tabBar.zqTabBar.hidden=YES;
 44 //    self.view.backgroundColor=ZQColor(232, 232, 232);
 45 
 46 }
 47 -(void)viewWillDisappear:(BOOL)animated
 48 {
 49     [super viewWillDisappear:animated];
 50     ZDYTabBarController *tabBar=(ZDYTabBarController*)self.navigationController.tabBarController;
 51     tabBar.zqTabBar.hidden=NO;
 52 }
 53 
 54 #pragma mark-------配置导航栏
 55 
 56 -(void)deplyNavigationController
 57 {
 58     GustomViewController *navi = [GustomViewController new];
 59     self.navigationController.navigationBar.barTintColor=XZColor(22,173,249);
 60     [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
 61                                                                       NSForegroundColorAttributeName:[UIColor whiteColor]}];
 62     UIButton *diqubutton=[UIButton buttonWithType:UIButtonTypeCustom];
 63     [diqubutton  setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
 64     [diqubutton  addTarget:self action:@selector(diquButtonClick) forControlEvents:UIControlEventTouchUpInside];
 65     diqubutton.titleLabel.font=[UIFont boldSystemFontOfSize:18];
 66     diqubutton.frame=CGRectMake(0, 0,20,20);
 67     UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton];
 68     
 69     // 调整 leftBarButtonItem 在 iOS7 下面的位置
 70     if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0))
 71         
 72     {
 73         UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
 74                                                                                        target:nil action:nil];
 75         negativeSpacer.width = -10;//这个数值可以根据情况自由变化
 76         self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton];
 77     }
 78     else
 79         
 80         self.navigationItem.leftBarButtonItem = leftbutton;
 81     
 82     self.tabBarController.tabBar.hidden=YES;
 83 }
 84 -(void)diquButtonClick
 85 {
 86     [self.navigationController popViewControllerAnimated:YES];
 87     
 88 }
 89 - (void)didReceiveMemoryWarning {
 90     [super didReceiveMemoryWarning];
 91     // Dispose of any resources that can be recreated.
 92 }
 93 
 94 /*
 95 #pragma mark - Navigation
 96 
 97 // In a storyboard-based application, you will often want to do a little preparation before navigation
 98 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 99     // Get the new view controller using [segue destinationViewController].
100     // Pass the selected object to the new view controller.
101 }
102 */
103 
104 @end

自定义GuestomViewController,

.m

 

  1 //
  2 //  GustomViewController.m
  3 //  暇课
  4 //
  5 //  Created by mac on 15/12/16.
  6 //  Copyright © 2015年 暇课. All rights reserved.
  7 //
  8 
  9 #import "GustomViewController.h"
 10 
 11 @interface GustomViewController ()<UINavigationControllerDelegate, UIGestureRecognizerDelegate>
 12 
 13 @end
 14 
 15 @implementation GustomViewController
 16 
 17 
 18 - (instancetype)initWithRootViewController:(UIViewController *)rootViewController
 19  {
 20     if (self = [super initWithRootViewController:rootViewController]) {
 21             // 设置navigationBar的背景颜色,根据需要自己设置
 22             self.navigationBar.barTintColor = XZColor(22,173,249);
 23             // 设置navigationBar是否透明,不透明的话会使可用界面原点下移(0,0)点为导航栏左下角下方的那个点
 24             self.navigationBar.translucent = NO;
 25             // 设置navigationBar是不是使用系统默认返回,默认为YES
 26             self.interactivePopGestureRecognizer.enabled = YES;
 27             // 创建一个颜色,便于之后设置颜色使用
 28             UIColor * color = [UIColor whiteColor];
 29             // 设置navigationBar元素的背景颜色,不包括title
 30             self.navigationBar.tintColor = color;
 31             // 设置navigationController的title的字体颜色
 32             NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
 33             self.navigationBar.titleTextAttributes = dict;
 34         
 35         self.navigationBar.barTintColor=XZColor(22,173,249);
 36         [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],
 37                                                                           NSForegroundColorAttributeName:[UIColor whiteColor]}];
 38         UIButton *diqubutton=[UIButton buttonWithType:UIButtonTypeCustom];
 39         [diqubutton  setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
 40         [diqubutton  addTarget:self action:@selector(diquButtonClick) forControlEvents:UIControlEventTouchUpInside];
 41         diqubutton.titleLabel.font=[UIFont boldSystemFontOfSize:18];
 42         diqubutton.frame=CGRectMake(0, 0,20,20);
 43         UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton];
 44         
 45         // 调整 leftBarButtonItem 在 iOS7 下面的位置
 46         if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0))
 47             
 48         {
 49             UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
 50                                                                                            target:nil action:nil];
 51             negativeSpacer.width = -10;//这个数值可以根据情况自由变化
 52             self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftbutton];
 53         }
 54         else
 55             
 56             self.navigationItem.leftBarButtonItem = leftbutton;
 57         
 58         self.tabBarController.tabBar.hidden=YES;
 59         }
 60     
 61      return self;
 62 }
 63 
 64 //- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
 65 //{
 66 //    // fix ‘nested pop animation can result in corrupted navigation bar‘
 67 //    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 68 //        self.interactivePopGestureRecognizer.enabled = NO;
 69 //    }
 70 //    
 71 //    [super pushViewController:viewController animated:animated];
 72 //}
 73 //
 74 //- (void)navigationController:(UINavigationController *)navigationController
 75 //       didShowViewController:(UIViewController *)viewController
 76 //                    animated:(BOOL)animated
 77 //{
 78 //    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 79 //        navigationController.interactivePopGestureRecognizer.enabled = YES;
 80 //    }
 81 //}
 82 
 83 - (void)viewDidLoad {
 84     [super viewDidLoad];
 85     
 86     
 87     // 为self创建弱引用对象
 88     __weak typeof (self) weakSelf = self;
 89     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
 90         self.interactivePopGestureRecognizer.delegate = weakSelf;
 91     }
 92 }
 93 
 94 - (void)didReceiveMemoryWarning {
 95     [super didReceiveMemoryWarning];
 96     // Dispose of any resources that can be recreated.
 97 }
 98 
 99 /*
100 #pragma mark - Navigation
101 
102 // In a storyboard-based application, you will often want to do a little preparation before navigation
103 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
104     // Get the new view controller using [segue destinationViewController].
105     // Pass the selected object to the new view controller.
106 }
107 */
108 
109 @end

 

完成~

ios 自定义导航栏,开启侧滑返回手势

标签:

原文地址:http://www.cnblogs.com/damnbird/p/5050332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!