cdts/xdts-ios 3/TreeHole/Code/Base/PYNavigationViewController.m
2023-07-27 09:20:00 +08:00

275 lines
12 KiB
Objective-C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// EHNavigationViewController.m
// ExpressHelper
//
// Created by ko1o on 2019/3/7.
// Copyright © 2019年 ko1o. All rights reserved.
//
#import "PYNavigationViewController.h"
#import "PYBaseViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
@interface PYNavigationViewController ()<UINavigationControllerDelegate>
@end
@implementation PYNavigationViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.interactivePopGestureRecognizer addTarget:self action:@selector(handlePopGesture:)];
}
+ (void)initialize
{
[self customizeInterface];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.fd_fullscreenPopGestureRecognizer.enabled = NO; //禁用全屏手势引入FDFullscreen是优化导航栏隐藏时切换界面系统的右滑动画体验不佳
self.delegate = self;
}
return self;
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return self.topViewController.preferredStatusBarStyle;
}
- (UIModalPresentationStyle)modalPresentationStyle {
return UIModalPresentationFullScreen;
}
#pragma mark - UINavigationControllerDelegate
- (void)updateTitleTextAttributesWithTopVC
{
self.navigationBar.barStyle = self.topViewController.mt_barStyle;
self.navigationBar.titleTextAttributes = self.topViewController.mt_titleTextAttributes;
}
- (void)updateNavigationBarForViewController:(UIViewController *)vc
{
[vc mt_applyAppearanceToNavigationBar:self.navigationBar];
}
- (void)transitionNavigationBarStyleWithCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator viewController:(UIViewController * _Nonnull)viewController {
UIViewController *from = [coordinator viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *to = [coordinator viewControllerForKey:UITransitionContextToViewControllerKey];
if (self == to) { // to == self
[self updateNavigationBarForViewController:viewController];
}
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self updateNavigationBarForViewController:viewController];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
if (context.isCancelled) {
[self updateNavigationBarForViewController:from];
} else {
[self updateNavigationBarForViewController:viewController];
}
}];
if (@available(iOS 10.0, *)) {
[coordinator notifyWhenInteractionChangesUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
;
}];
} else {
[coordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
;
}];
}
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
// NSLog(@"willShowViewController %@", viewController);
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator;
if (coordinator) {
[self transitionNavigationBarStyleWithCoordinator:coordinator viewController:viewController];
} else {
[self updateNavigationBarForViewController:viewController];
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
// NSLog(@"didShowViewController %@", viewController);
navigationController.interactivePopGestureRecognizer.enabled = NO;
if (navigationController.viewControllers.count > 1 && !viewController.fd_interactivePopDisabled) {
navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)viewController;
navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.childViewControllers.count == 1
&& viewController.hidesBottomBarWhenPushed == NO) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- (void)setNavigationBarHidden:(BOOL)navigationBarHidden {
[super setNavigationBarHidden:navigationBarHidden];
}
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated
{
[super setNavigationBarHidden:hidden animated:animated];
self.viewControllers.lastObject.fd_prefersNavigationBarHidden = hidden;
}
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated {
if (viewControllers.count > 1 && viewControllers.lastObject.hidesBottomBarWhenPushed == NO) {
viewControllers.lastObject.hidesBottomBarWhenPushed = YES;
}
[super setViewControllers:viewControllers animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
UIViewController *vc = [super popViewControllerAnimated:animated];
[self updateTitleTextAttributesWithTopVC];
return vc;
}
- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSArray *array = [super popToViewController:viewController animated:animated];
[self updateTitleTextAttributesWithTopVC];
return array;
}
- (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
NSArray *array = [super popToRootViewControllerAnimated:animated];
[self updateTitleTextAttributesWithTopVC];
return array;
}
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{
if (viewControllers.count > 1 && viewControllers.lastObject.hidesBottomBarWhenPushed == NO) {
viewControllers.lastObject.hidesBottomBarWhenPushed = YES;
}
[super setViewControllers:viewControllers];
}
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title textColor:(UIColor *)textColor target:(id)target action:(SEL) action
{
UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
textColor = textColor ?:[UIColor blackColor];
// 不必手动设置导航栏的tintColor会适配
// [item setTitleTextAttributes:@{NSForegroundColorAttributeName : textColor}
// forState:UIControlStateNormal];
// [item setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor mt_colorWithColor:textColor alphaScale:0.5] }
// forState:UIControlStateHighlighted];
return item;
}
+ (UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title target:(id)target action:(SEL) action
{
UIBarButtonItem * item = [self barButtonItemWithTitle:title textColor:[UIColor blackColor] target:target action:action];
return item;
}
+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage target:(id)target action:(SEL) action
{
UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:target action:action];
return item;
}
+ (void)customizeInterface
{
UINavigationBar *navigationBarAppearance = nil;
if (@available(iOS 9, *)) {
navigationBarAppearance = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[PYNavigationViewController class]]];
} else {
navigationBarAppearance = [UINavigationBar appearanceWhenContainedIn:[PYNavigationViewController class], nil];
}
UIImage *backgroundImage = ImageNamed(@"tabbar_backgound_image");
navigationBarAppearance.translucent = NO;
[navigationBarAppearance setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
//导航栏按钮风格
// Color不必手动设置导航栏的tintColor会适配
// UIColor * barButtonItemTitlecolor = [UIColor yellowColor];
// UIColor * barButtonItemTitlecolorHL = [UIColor mt_colorWithColor:barButtonItemTitlecolor alphaScale:0.5];
//
[[UIBarButtonItem appearance]setTitleTextAttributes:@{NSFontAttributeName : MT_FONT_MEDIUM_NO_SCALE_SIZE(20),
// NSForegroundColorAttributeName:barButtonItemTitlecolor,
}
forState:UIControlStateNormal];
[[UIBarButtonItem appearance]setTitleTextAttributes:@{NSFontAttributeName : MT_FONT_MEDIUM_NO_SCALE_SIZE(20),
// NSForegroundColorAttributeName:barButtonItemTitlecolorHL,
}
forState:UIControlStateHighlighted];
[[UIBarButtonItem appearance]setTitleTextAttributes:@{NSFontAttributeName : MT_FONT_MEDIUM_NO_SCALE_SIZE(20),
// NSForegroundColorAttributeName:barButtonItemTitlecolorHL,
}
forState:UIControlStateDisabled];
// UINavigationBar* navBar = [UINavigationBar appearance];
// //设置导航栏字体
// NSMutableDictionary* barDic = [NSMutableDictionary dictionary];
// barDic[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
// barDic[NSForegroundColorAttributeName] = COLOR_WITH_RGB(0xFFFFFF);
// [navBar setTitleTextAttributes:barDic];
//
}
UIColor* mt_blendColor(UIColor *from, UIColor *to, float percent) {
CGFloat fromRed = 0;
CGFloat fromGreen = 0;
CGFloat fromBlue = 0;
CGFloat fromAlpha = 0;
[from getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
CGFloat toRed = 0;
CGFloat toGreen = 0;
CGFloat toBlue = 0;
CGFloat toAlpha = 0;
[to getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
CGFloat newRed = fromRed + (toRed - fromRed) * fminf(1, percent * 4) ;
CGFloat newGreen = fromGreen + (toGreen - fromGreen) * fminf(1, percent * 4);
CGFloat newBlue = fromBlue + (toBlue - fromBlue) * fminf(1, percent * 4);
CGFloat newAlpha = fromAlpha + (toAlpha - fromAlpha) * fminf(1, percent * 4);
return [UIColor colorWithRed:newRed green:newGreen blue:newBlue alpha:newAlpha];
}
- (void)handlePopGesture:(UIScreenEdgePanGestureRecognizer *)recognizer {
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator;
PYBaseViewController *from = [coordinator viewControllerForKey:UITransitionContextFromViewControllerKey];
PYBaseViewController *to = [coordinator viewControllerForKey:UITransitionContextToViewControllerKey];
if ([from isKindOfClass:PYBaseViewController.class] && [to isKindOfClass:PYBaseViewController.class]) {
if (recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateChanged) {
self.navigationBar.tintColor = mt_blendColor(from.mt_nav_tintColor, to.mt_nav_tintColor, coordinator.percentComplete);
} else {
if (coordinator.isCancelled) {
self.navigationBar.tintColor = from.mt_nav_tintColor;
}
}
}
}
@end