Skip to content

Commit

Permalink
手势优化,增加显示系统导航栏的方法及逻辑#86
Browse files Browse the repository at this point in the history
  • Loading branch information
QuintGao committed Jun 9, 2021
1 parent d6cdcd3 commit d83d4f4
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 44 deletions.
2 changes: 1 addition & 1 deletion GKNavigationBar.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'GKNavigationBar'
s.version = '1.5.7'
s.version = '1.5.8'
s.license = 'MIT'
s.summary = '自定义导航栏--导航栏联动'
s.homepage = 'https://github.com/QuintGao/GKNavigationBar'
Expand Down
10 changes: 6 additions & 4 deletions GKNavigationBar/GestureHandle/GKPopAnimatedTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ - (void)animateTransition {
self.fromViewController.view.frame = CGRectMake(screenW, 0, screenW, screenH);
if (self.isScale) {
self.shadowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
}
if (@available(iOS 11.0, *)) {
toView.frame = CGRectMake(0, 0, screenW, screenH);
if (@available(iOS 11.0, *)) {
toView.frame = CGRectMake(0, 0, screenW, screenH);
}else {
toView.transform = CGAffineTransformIdentity;
}
}else {
toView.transform = CGAffineTransformIdentity;
toView.frame = CGRectMake(0, 0, screenW, screenH);
}
} completion:^(BOOL finished) {
[self completeTransition];
Expand Down
3 changes: 3 additions & 0 deletions GKNavigationBar/NavigationBar/GKNavigationBarConfigure.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign, readonly) CGFloat navItemLeftSpace;
@property (nonatomic, assign, readonly) CGFloat navItemRightSpace;

/// 用于恢复系统导航栏的显示,默认NO
@property (nonatomic, assign) BOOL gk_restoreSystemNavBar;

/// 单例,设置一次全局使用
+ (instancetype)sharedInstance;

Expand Down
28 changes: 14 additions & 14 deletions GKNavigationBar/NavigationBar/GKNavigationBarConfigure.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#import <sys/utsname.h>

/// 设备宽度,跟横竖屏无关
#define DEVICE_WIDTH MIN([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)
#define GK_DEVICE_WIDTH MIN([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)

/// 设备高度,跟横竖屏无关
#define DEVICE_HEIGHT MAX([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)
#define GK_DEVICE_HEIGHT MAX([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)

@interface GKNavigationBarConfigure()

Expand Down Expand Up @@ -68,13 +68,13 @@ - (void)updateConfigure:(void (^)(GKNavigationBarConfigure * _Nonnull))block {
}

- (UIViewController *)visibleViewController {
return [[GKNavigationBarConfigure keyWindow].rootViewController gk_visibleViewControllerIfExist];
return [[GKNavigationBarConfigure keyWindow].rootViewController gk_findCurrentViewControllerIsRoot:YES];
}

- (CGFloat)gk_fixedSpace {
// 经测试发现iPhone 12和iPhone 12 Pro,默认导航栏间距是16,需要单独处理
if ([GKNavigationBarConfigure is61InchScreenAndiPhone12]) return 16;
return DEVICE_WIDTH > 375.0f ? 20 : 16;
return GK_DEVICE_WIDTH > 375.0f ? 20 : 16;
}

- (NSBundle *)gk_libraryBundle {
Expand Down Expand Up @@ -223,7 +223,7 @@ + (BOOL)isRegularScreen {
static NSInteger is67InchScreen = -1;
+ (BOOL)is67InchScreen {
if (is67InchScreen < 0) {
is67InchScreen = (DEVICE_WIDTH == self.screenSizeFor67Inch.width && DEVICE_HEIGHT == self.screenSizeFor67Inch.height) ? 1 : 0;
is67InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor67Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor67Inch.height) ? 1 : 0;
}
return is67InchScreen > 0;
}
Expand All @@ -233,23 +233,23 @@ + (BOOL)is65InchScreen {
if (is65InchScreen < 0) {
// Since iPhone XS Max、iPhone 11 Pro Max and iPhone XR share the same resolution, we have to distinguish them using the model identifiers
// 由于 iPhone XS Max、iPhone 11 Pro Max 这两款机型和 iPhone XR 的屏幕宽高是一致的,我们通过机器 Identifier 加以区别
is65InchScreen = (DEVICE_WIDTH == self.screenSizeFor65Inch.width && DEVICE_HEIGHT == self.screenSizeFor65Inch.height && ([[self deviceModel] isEqualToString:@"iPhone11,4"] || [[self deviceModel] isEqualToString:@"iPhone11,6"] || [[self deviceModel] isEqualToString:@"iPhone12,5"])) ? 1 : 0;
is65InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor65Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor65Inch.height && ([[self deviceModel] isEqualToString:@"iPhone11,4"] || [[self deviceModel] isEqualToString:@"iPhone11,6"] || [[self deviceModel] isEqualToString:@"iPhone12,5"])) ? 1 : 0;
}
return is65InchScreen > 0;
}

static NSInteger is61InchScreenAndiPhone12 = -1;
+ (BOOL)is61InchScreenAndiPhone12 {
if (is61InchScreenAndiPhone12 < 0) {
is61InchScreenAndiPhone12 = (DEVICE_WIDTH == self.screenSizeFor61InchAndiPhone12.width && DEVICE_HEIGHT == self.screenSizeFor61InchAndiPhone12.height && ([[self deviceModel] isEqualToString:@"iPhone13,2"] || [[self deviceModel] isEqualToString:@"iPhone13,3"])) ? 1 : 0;
is61InchScreenAndiPhone12 = (GK_DEVICE_WIDTH == self.screenSizeFor61InchAndiPhone12.width && GK_DEVICE_HEIGHT == self.screenSizeFor61InchAndiPhone12.height && ([[self deviceModel] isEqualToString:@"iPhone13,2"] || [[self deviceModel] isEqualToString:@"iPhone13,3"])) ? 1 : 0;
}
return is61InchScreenAndiPhone12 > 0;
}

static NSInteger is61InchScreen = -1;
+ (BOOL)is61InchScreen {
if (is61InchScreen < 0) {
is61InchScreen = (DEVICE_WIDTH == self.screenSizeFor61Inch.width && DEVICE_HEIGHT == self.screenSizeFor61Inch.height && ([[self deviceModel] isEqualToString:@"iPhone11,8"] || [[self deviceModel] isEqualToString:@"iPhone12,1"])) ? 1 : 0;
is61InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor61Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor61Inch.height && ([[self deviceModel] isEqualToString:@"iPhone11,8"] || [[self deviceModel] isEqualToString:@"iPhone12,1"])) ? 1 : 0;
}
return is61InchScreen > 0;
}
Expand All @@ -259,15 +259,15 @@ + (BOOL)is58InchScreen {
if (is58InchScreen < 0) {
// Both iPhone XS and iPhone X share the same actual screen sizes, so no need to compare identifiers
// iPhone XS 和 iPhone X 的物理尺寸是一致的,因此无需比较机器 Identifier
is58InchScreen = (DEVICE_WIDTH == self.screenSizeFor58Inch.width && DEVICE_HEIGHT == self.screenSizeFor58Inch.height) ? 1 : 0;
is58InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor58Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor58Inch.height) ? 1 : 0;
}
return is58InchScreen > 0;
}

static NSInteger is55InchScreen = -1;
+ (BOOL)is55InchScreen {
if (is55InchScreen < 0) {
is55InchScreen = (DEVICE_WIDTH == self.screenSizeFor55Inch.width && DEVICE_HEIGHT == self.screenSizeFor55Inch.height) ? 1 : 0;
is55InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor55Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor55Inch.height) ? 1 : 0;
}
return is55InchScreen > 0;
}
Expand All @@ -278,31 +278,31 @@ + (BOOL)is55InchScreen {
+ (BOOL)is54InchScreen {
if (is54InchScreen < 0) {
// iPhone XS 和 iPhone X 的物理尺寸是一致的,因此无需比较机器 Identifier
is54InchScreen = (DEVICE_WIDTH == self.screenSizeFor54Inch.width && DEVICE_HEIGHT == self.screenSizeFor54Inch.height) ? 1 : 0;
is54InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor54Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor54Inch.height) ? 1 : 0;
}
return is54InchScreen > 0;
}

static NSInteger is47InchScreen = -1;
+ (BOOL)is47InchScreen {
if (is47InchScreen < 0) {
is47InchScreen = (DEVICE_WIDTH == self.screenSizeFor47Inch.width && DEVICE_HEIGHT == self.screenSizeFor47Inch.height) ? 1 : 0;
is47InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor47Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor47Inch.height) ? 1 : 0;
}
return is47InchScreen > 0;
}

static NSInteger is40InchScreen = -1;
+ (BOOL)is40InchScreen {
if (is40InchScreen < 0) {
is40InchScreen = (DEVICE_WIDTH == self.screenSizeFor40Inch.width && DEVICE_HEIGHT == self.screenSizeFor40Inch.height) ? 1 : 0;
is40InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor40Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor40Inch.height) ? 1 : 0;
}
return is40InchScreen > 0;
}

static NSInteger is35InchScreen = -1;
+ (BOOL)is35InchScreen {
if (is35InchScreen < 0) {
is35InchScreen = (DEVICE_WIDTH == self.screenSizeFor35Inch.width && DEVICE_HEIGHT == self.screenSizeFor35Inch.height) ? 1 : 0;
is35InchScreen = (GK_DEVICE_WIDTH == self.screenSizeFor35Inch.width && GK_DEVICE_HEIGHT == self.screenSizeFor35Inch.height) ? 1 : 0;
}
return is35InchScreen > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ NS_ASSUME_NONNULL_BEGIN
/// @param sender sender
- (void)backItemClick:(id)sender;

/// 获取当前controller里的最高层可见viewController(可见的意思是还会判断self.view.window是否存在)
- (UIViewController *)gk_visibleViewControllerIfExist;
/// 查找当前显示的控制器
/// @param isRoot 是否是根控制器
- (UIViewController *)gk_findCurrentViewControllerIsRoot:(BOOL)isRoot;

@end

Expand Down
95 changes: 77 additions & 18 deletions GKNavigationBar/NavigationBar/UIViewController+GKNavigationBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ + (void)load {
- (void)gk_viewDidLoad {
// bug fix #76,修改添加了子控制器后调整导航栏间距无效的bug
// 当创建了gk_navigationBar或者父控制器是导航控制器的时候才去调整导航栏间距
if ([self navItemSpaceChangeIfNeeded]) {
if ([self shouldHandleNavBar]) {
// 设置默认导航栏间距
self.gk_navItemLeftSpace = GKNavigationBarItemSpace;
self.gk_navItemRightSpace = GKNavigationBarItemSpace;
Expand All @@ -70,19 +70,21 @@ - (void)gk_viewWillAppear:(BOOL)animated {

if (self.gk_NavBarInit) {
// 隐藏系统导航栏
if (!self.navigationController.gk_openSystemNavHandle && !self.navigationController.isNavigationBarHidden) {
[self.navigationController setNavigationBarHidden:YES];
if (!self.navigationController.gk_openSystemNavHandle) {
[self hiddenSystemNavBar];
}

// 将自定义导航栏放置顶层
if (self.gk_navigationBar && !self.gk_navigationBar.hidden) {
[self.view bringSubviewToFront:self.gk_navigationBar];
}
}else {
[self restoreSystemNavBar];
}

// bug fix #76,修改添加了子控制器后调整导航栏间距无效的bug
// 当创建了gk_navigationBar或者父控制器是导航控制器的时候才去调整导航栏间距
if ([self navItemSpaceChangeIfNeeded] && !self.gk_disableFixNavItemSpace) {
if ([self shouldHandleNavBar] && !self.gk_disableFixNavItemSpace) {
// 每次控制器出现的时候重置导航栏间距
if (self.gk_navItemLeftSpace == GKNavigationBarItemSpace) {
self.gk_navItemLeftSpace = GKConfigure.navItemLeftSpace;
Expand All @@ -105,8 +107,10 @@ - (void)gk_viewWillAppear:(BOOL)animated {
}

- (void)gk_viewDidAppear:(BOOL)animated {
if (self.gk_NavBarInit && !self.navigationController.isNavigationBarHidden) {
[self.navigationController setNavigationBarHidden:YES];
if (self.gk_NavBarInit) {
[self hiddenSystemNavBar];
}else {
[self restoreSystemNavBar];
}
[self gk_viewDidAppear:animated];
}
Expand Down Expand Up @@ -500,22 +504,63 @@ - (void)backItemClick:(id)sender {
#endif
}

- (UIViewController *)gk_visibleViewControllerIfExist {
if (self.presentedViewController) {
return [self.presentedViewController gk_visibleViewControllerIfExist];
- (UIViewController *)gk_findCurrentViewControllerIsRoot:(BOOL)isRoot {
if ([self canFindPresentedViewController:self.presentedViewController]) {
return [self.presentedViewController gk_findCurrentViewControllerIsRoot:NO];
}
if ([self isKindOfClass:[UITabBarController class]]) {
return [[(UITabBarController *)self selectedViewController] gk_findCurrentViewControllerIsRoot:NO];;
}
if ([self isKindOfClass:[UINavigationController class]]) {
return [((UINavigationController *)self).topViewController gk_visibleViewControllerIfExist];
[[(UINavigationController *)self topViewController] gk_findCurrentViewControllerIsRoot:NO];
}
if ([self isKindOfClass:[UITabBarController class]]) {
return [((UITabBarController *)self).selectedViewController gk_visibleViewControllerIfExist];
if (self.childViewControllers.count > 0) {
if (self.childViewControllers.count == 1 && isRoot) {
return [self.childViewControllers.firstObject gk_findCurrentViewControllerIsRoot:NO];
}else {
__block UIViewController *currentViewController = self;
// 从最上层遍历(逆序),查找正在显示的UITabBarController 或 UINavigationController 类型的
// 是否包含 UITabBarController 或 UINavigationController 类全屏显示的 controller
[self.childViewControllers enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 判断obj.view 是否加载,如果尚未加载,调用 obj.view 会触发 viewDidLoad
if (obj.isViewLoaded) {
CGPoint point = [obj.view convertPoint:CGPointZero toView:nil];
CGSize windowSize = obj.view.window.bounds.size;
// 正在全屏显示
BOOL isFullScreenShow = !obj.view.hidden && obj.view.alpha > 0.01 && CGPointEqualToPoint(point, CGPointZero) && CGSizeEqualToSize(obj.view.bounds.size, windowSize);
// 判断类型
BOOL isStopFindController = [obj isKindOfClass:UINavigationController.class] || [obj isKindOfClass:UITabBarController.class];
if (isFullScreenShow && isStopFindController) {
currentViewController = [obj gk_findCurrentViewControllerIsRoot:NO];
*stop = YES;
}
}
}];
return currentViewController;
}
}else if ([self respondsToSelector:NSSelectorFromString(@"contentViewController")]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
UIViewController *tempViewController = [self performSelector:NSSelectorFromString(@"contentViewController")];
#pragma clang diagnostic pop
if (tempViewController) {
return [tempViewController gk_findCurrentViewControllerIsRoot:NO];
}
}
if ([self isViewLoaded] && self.view.window) {
return self;
}else {
NSLog(@"找不到可见的控制器,viewcontroller.self = %@,self.view.window=%@", self, self.view.window);
return nil;
return self;
}

- (BOOL)canFindPresentedViewController:(UIViewController *)viewController {
if (!viewController) {
return NO;
}
if ([viewController isKindOfClass:UIAlertController.class]) {
return NO;
}
if ([@"_UIContextMenuActionsOnlyViewController" isEqualToString:NSStringFromClass(viewController.class)]) {
return NO;
}
return YES;
}

#pragma mark - Private Methods
Expand Down Expand Up @@ -625,7 +670,21 @@ - (BOOL)checkFixNavItemSpace {
return exist;
}

- (BOOL)navItemSpaceChangeIfNeeded {
- (void)hiddenSystemNavBar {
if (!self.navigationController.isNavigationBarHidden) {
[self.navigationController setNavigationBarHidden:YES];
}
}

- (void)restoreSystemNavBar {
if (GKConfigure.gk_restoreSystemNavBar && [self shouldHandleNavBar]) {
if (self.navigationController.isNavigationBarHidden) {
[self.navigationController setNavigationBarHidden:NO];
}
}
}

- (BOOL)shouldHandleNavBar {
return self.gk_NavBarInit || [self.parentViewController isKindOfClass:[UINavigationController class]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
794AFC36242F4FB600BECA1B /* GKBaseNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GKBaseNavigationController.m; sourceTree = "<group>"; };
79672E89236EC04A0054300B /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text.yaml; name = .travis.yml; path = ../../.travis.yml; sourceTree = "<group>"; };
79672E8A236EC0520054300B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../../README.md; sourceTree = "<group>"; };
79672E8C236EC0740054300B /* GKNavigationBar.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = GKNavigationBar.podspec; path = ../../GKNavigationBar.podspec; sourceTree = "<group>"; };
79672E8C236EC0740054300B /* GKNavigationBar.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = GKNavigationBar.podspec; path = ../../GKNavigationBar.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
79672E93236EC8EB0054300B /* GKNavigationBar.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = GKNavigationBar.framework; sourceTree = BUILT_PRODUCTS_DIR; };
79672E96236EC8EB0054300B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
796CF7EC236D781400983E9B /* GKNavigationBarExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GKNavigationBarExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down
9 changes: 8 additions & 1 deletion GKNavigationBarExample/GKNavigationBarExample/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
configure.gk_navItemLeftSpace = 10.0f;
configure.gk_navItemRightSpace = 10.0f;

configure.shiledItemSpaceVCs = @[NSClassFromString(@"TZPhotoPickerController"), @"TZAlbumPickerController", @"TZ"];
configure.gk_restoreSystemNavBar = YES;

// configure.shiledItemSpaceVCs = @[NSClassFromString(@"TZPhotoPickerController"), @"TZAlbumPickerController", @"TZ"];
configure.shiledItemSpaceVCs = @[@"UIActivityViewController", @"UIActivityContentViewController"];
}];

[GKGestureConfigure setupCustomConfigure:^(GKGestureHandleConfigure * _Nonnull configure) {
Expand All @@ -57,6 +60,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

// [UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
// [UISearchBar appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
// [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

return YES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,22 @@ - (IBAction)navBarAlphaAction:(id)sender {
}

- (void)moreAction {
GKDemoWebViewController *webVC = [GKDemoWebViewController new];
[self.navigationController pushViewController:webVC animated:YES];
// GKDemoWebViewController *webVC = [GKDemoWebViewController new];
// [self.navigationController pushViewController:webVC animated:YES];
NSString *textToShare = @"GKNavigationBar是一个自定义导航栏";

UIImage *imageToShare = [UIImage imageNamed:@"Activity_selected"];

NSURL *urlToShare = [NSURL URLWithString:@"https://github.com/QuintGao/GKNavigationBar"];

NSArray *activityItems = @[textToShare, imageToShare, urlToShare];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

//不出现在活动项目
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];

[self presentViewController:activityVC animated:YES completion:nil];
}

#pragma mark - 懒加载
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ - (void)viewDidLoad {
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:NO];
// [self.navigationController setNavigationBarHidden:NO];
}

- (void)click {
Expand Down
Loading

0 comments on commit d83d4f4

Please sign in to comment.