您的位置:首页 > 移动开发 > IOS开发

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

2015-12-16 10:32 555 查看
自定义一个常用ListViewController

.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


完成~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: