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

自定义一个常用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

完成~

时间: 2024-10-19 18:47:21

ios 自定义导航栏,开启侧滑返回手势的相关文章

IOS 自定义导航栏标题和返回按钮标题

IOS中自定义导航栏标题: UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)]; titleText.backgroundColor = [UIColor clearColor]; titleText.textColor=[UIColor whiteColor]; [titleText setFont:[UIFont systemFontOfSize:17.0]]; [titleTex

iOS 自定义导航栏笔记

一.UINavigationBar的结构 导航栏几乎是每个页面都会碰到的问题,一般两种处理方式:1.隐藏掉不显示 2.自定义 1. 添加导航栏 TestViewController * mainVC = [[TestViewController alloc] init]; UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:mainVC]; self.window.ro

ios 自定义导航栏和分割线

自定义导航栏: // CustomNaviBarView.h #import <UIKit/UIKit.h> @interface CustomNaviBarView : UIView { @private /** * 左侧按钮 */ UIButton* _leftButton; /** * 右侧按钮 */ UIButton* _rightButton; /** * 中部标签 */ UILabel* _navTitle; } @property(nonatomic,strong)UIButto

IOS 自定义导航栏

我们自己新建一个View,来自定义导航栏,如下代码: #import <UIKit/UIKit.h> @interface CustomNavigation : UIView typedef enum { customEventClickLBtn1 ,//点击了最左边的按钮 customEventClickRBtn1 , //最右边的按钮 customEventClickLBtn2 ,//点击了左边第二个按钮 customEventClickRBtn2 //点击了右边最靠左的按钮 }Custo

iOS 自定义导航栏的返回按钮

UIBarButtonItem * item  =    [UIBarButtonItem appearance]; UIImage* image = [UIImage imageNamed:@"back_icon"]; [item setBackButtonBackgroundImage:[image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)] forState:UIControlS

自定义导航栏之滑动返回

以前我们所有的滑动返回,只是系统自带的滑动返回,只能在屏幕的左侧滑动才能到达效果. 但是QQ,新浪微博之类的应用,有在屏幕中间滑动也能返回的效果. 下面我们来看具体的实现代码: 我需要创建一个类继承 UINavigationController OC语言实现: #import "BaseNavigationController.h" @interface BaseNavigationController () @end @implementation BaseNavigationCon

IOS 自定义导航栏背景

//- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; -(id)init { self=[super init]; if(self) { UINavigationBar *navBar=[self navigationBar]; if([navBar respondsTo

实际iOS编程中遇到的自定义导航栏按钮,导致手势返回失效的解决方法

1\在实际编程过程中往往需要自定义导航栏上面的按钮,也就用: - (instancetype)initWithCustomView:(UIView *)customView; 但用了这个方法后可能会导致iOS7,8的手势返回失效,解决方法就是在自定义的导航栏的viewDidLoad方法中添加如下代码 注意:只有用系统的导航栏,或者继承于系统的导航栏才可以用Push方法,并且自带返回手势. - (void)viewDidLoad { [super viewDidLoad]; __weak type

微信小程序自定义导航栏(wx_custom_navigation_bar) 自定义返回键、首页键,动态设置标题,响应式组件

微信小程序自定义导航栏 navigation bar 返回键 首页 github: https://github.com/chen-yt/wx_custom_navigation_bar https://github.com/Superman2113/wx_custom_navigation_bar 代码 navbar组件 navbar.wxml <view class="navbar" style="{{'height: ' + navigationBarHeight