IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
来源:互联网 发布:jsp导入java类 编辑:程序博客网 时间:2024/05/21 22:23
- //
- // BaseViewController.h
- // TabBarTest
- //
- // Created by apple on 15/11/27.
- // Copyright (c) 2015年 zwhy. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- typedef enum : NSUInteger {
- NavBarStyleBlack,//黑色背景
- NavBarStyleWhite//白色背景
- } NavBarStyle;
- @interface BaseViewController : UIViewController
- @property(nonatomic,assign) BOOL hideStatusBar;
- @property(nonatomic,assign) BOOL hideNavigationBar;
- @property (nonatomic, assign) NavBarStyle navBarStyle;
- - (void)hideTabBar;
- - (void)showTabBar;
- @end
- //
- // BaseViewController.m
- // TabBarTest
- //
- // Created by apple on 15/11/27.
- // Copyright (c) 2015年 zwhy. All rights reserved.
- //
- #import "BaseViewController.h"
- #import "UIViewController+BackButtonHandler.h"
- @interface BaseViewController ()
- @end
- @implementation BaseViewController
- -(void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- NSLog(@"###%s",__FUNCTION__);
- [[UIApplication sharedApplication] setStatusBarHidden:self.hideStatusBar==YES?YES:NO];
- [[UIApplication sharedApplication] setStatusBarStyle:self.navBarStyle==NavBarStyleWhite?UIStatusBarStyleDefault:UIStatusBarStyleLightContent];
- //隐藏导航栏
- [self.navigationController setNavigationBarHidden:self.hideNavigationBar==YES?YES:NO];
- [self initNavBar];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- [[UIApplication sharedApplication] setStatusBarHidden:NO];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
- [self.navigationController setNavigationBarHidden:NO];
- //显示导航栏
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor=[UIColor whiteColor];
- NSLog(@"###%s",__FUNCTION__);
- }
- #pragma mark - Nav
- -(void)initNavBar
- {
- //定义导航栏
- if (self.navBarStyle==NavBarStyleWhite) {
- NSLog(@"### %s NavBarStyleWhite...",__FUNCTION__);
- [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
- [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
- [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
- } else {//NavBarStyleBlack
- NSLog(@"### %s NavBarStyleBlack...",__FUNCTION__);
- [self.navigationController.navigationBar setBarTintColor:kColorTextGray_51];
- [self.navigationController.navigationBar setBackgroundColor:kColorTextGray_51];
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
- [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
- }
- //自定义返回按钮
- UIImage *backButtonImage = [[UIImage imageNamed:@"nav_btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 40, 0, 0)];
- [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
- //隐藏返回按钮的文字方法一
- UIBarButtonItem *buttonItem = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil nil];
- UIOffset offset;
- offset.horizontal = -500;
- [buttonItem setBackButtonTitlePositionAdjustment:offset forBarMetrics:UIBarMetricsDefault];
- //隐藏返回按钮的文字方法二
- // UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(turnBack:)];
- // self.navigationItem.backBarButtonItem = item;
- }
- -(void)turnBack:(id)sender
- {
- NSLog(@"%s",__FUNCTION__);
- }
- #pragma mark - TabBar
- - (void)hideTabBar
- {
- if (self.tabBarController.tabBar.hidden == YES) {
- return;
- }
- UIView *contentView;
- if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
- contentView = [self.tabBarController.view.subviews objectAtIndex:1];
- else
- contentView = [self.tabBarController.view.subviews objectAtIndex:0];
- contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
- self.tabBarController.tabBar.hidden = YES;
- }
- - (void)showTabBar
- {
- if (self.tabBarController.tabBar.hidden == NO)
- {
- return;
- }
- UIView *contentView;
- if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
- contentView = [self.tabBarController.view.subviews objectAtIndex:1];
- else
- contentView = [self.tabBarController.view.subviews objectAtIndex:0];
- contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
- self.tabBarController.tabBar.hidden = NO;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- @end
- // UIViewController+BackButtonHandler.h
- //
- // Created by Sergey Nikitenko on 10/1/13.
- // Copyright 2013 Sergey Nikitenko. All rights reserved.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #import <UIKit/UIKit.h>
- @protocol BackButtonHandlerProtocol <NSObject>
- @optional
- // Override this method in UIViewController derived class to handle 'Back' button click
- -(BOOL)navigationShouldPopOnBackButton;
- @end
- @interface UIViewController (BackButtonHandler) <BackButtonHandlerProtocol>
- @end
- //
- // UIViewController+BackButtonHandler.m
- //
- // Created by Sergey Nikitenko on 10/1/13.
- // Copyright 2013 Sergey Nikitenko. All rights reserved.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #import "UIViewController+BackButtonHandler.h"
- @implementation UIViewController (BackButtonHandler)
- @end
- @implementation UINavigationController (ShouldPopOnBackButton)
- - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
- if([self.viewControllers count] < [navigationBar.items count]) {
- return YES;
- }
- BOOL shouldPop = YES;
- UIViewController* vc = [self topViewController];
- if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
- shouldPop = [vc navigationShouldPopOnBackButton];
- }
- if(shouldPop) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [self popViewControllerAnimated:YES];
- });
- } else {
- // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906
- for(UIView *subview in [navigationBar subviews]) {
- if(subview.alpha < 1.) {
- [UIView animateWithDuration:.25 animations:^{
- subview.alpha = 1.;
- }];
- }
- }
- }
- return NO;
- }
- @end
- #pragma mark - BackButtonHandlerProtocol
- -(BOOL)navigationShouldPopOnBackButton
- {
- NSLog(@"%s AAAAA",__FUNCTION__);
- if ([self.delegate respondsToSelector:@selector(workExperienceDidChange:)]) {
- [self.delegate workExperienceDidChange:self.workList];
- }
- return YES;
- }
0 0
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- 【IOS】隐藏导航栏,改变状态栏背景色
- [iOS 自定义导航栏/导航条]隐藏系统导航条
- ios之导航渐变---/导航透明/隐藏导航栏以及手势返回遇到的问题,状态栏
- 导航栏背景色、标题颜色以及返回键自定义
- iOS--修改导航栏的背景色
- Android动态控制状态栏以及系统导航栏显示和隐藏
- 自定义导航栏返回按钮点击事件
- 修改系统导航栏背景色和标题颜色
- 隐藏android系统的状态栏和导航栏
- 隐藏android系统的状态栏和导航栏
- 改变iOS中的导航栏和状态栏 的背景色和字体颜色
- iOS系统导航栏的自定义化
- 导航栏背景色改变
- iOS导航栏和底部tabbar的隐藏和背景色
- 设置导航栏的背景色
- 状态栏 导航栏 tabbar背景色&字体颜色设置
- post/get解决中文乱码
- 一个类怎样调用另一个类中的属性和方法
- \r,\n的作用
- Unable to execute dex: Multiple dex files define 解决方法
- ios 访问版本信息 { "resultCount":0, "results": [] }
- IOS自定义系统导航栏:导航栏背景色、状态栏样式以及是否隐藏、拦截系统导航栏的返回点击事件
- IFS8.2、安全检查点介绍、开发与配置
- oracle_DDL
- id取模分表分表
- Android设置头像,手机拍照或从本地相册选取图片作为头像
- linux下使用脚本杀死某个进程
- 论文开题报告
- UUI Make StartUp Disk
- 由static制作累加器