//
// 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];
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;
}