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

自定义一个常用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(, , );
}
- (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(,,);
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:],
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:];
diqubutton.frame=CGRectMake(, ,,);
UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton]; // 调整 leftBarButtonItem 在 iOS7 下面的位置
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?:)) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -;//这个数值可以根据情况*变化
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(,,);
// 设置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(,,);
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:],
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:];
diqubutton.frame=CGRectMake(, ,,);
UIBarButtonItem *leftbutton=[[UIBarButtonItem alloc]initWithCustomView:diqubutton]; // 调整 leftBarButtonItem 在 iOS7 下面的位置
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?:)) {
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -;//这个数值可以根据情况*变化
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

完成~

上一篇:Guava初识


下一篇:codeforce 804B Minimum number of steps