🔥 NXNavigationExtension is a simple, easy-to-use navigation bar handling framework designed for iOS applications. The framework has minimal intrusion into existing code and can be used for most application scenarios with a few simple method calls. Both NXNavigationExtension and the sample program code have been adapted for Dark Mode.

🌟 Start using

Download the NXNavigationExtension sample code.

Install using CocoaPods

To integrate NXNavigationExtension into an Xcode project using CocoaPods, you need to specify in your Podfile:

pod 'NXNavigationExtension'
Copy the code

Install using Carthage

Carthage is a decentralized package manager that builds dependencies and provides you with a binary framework. To integrate NXNavigationExtension, add the following to your Cartfile file:

github "l1Dan/NXNavigationExtension"
Copy the code

🌈 requirements

The latest version supports iOS 9.0 at the minimum

NXNavigationExtension Version Minimum iOS Target Minimum macOS Target Minimum watchOS Target Minimum tvOS Target Notes
3.x IOS 9.0 MacOS 10.15 n/a n/a macOS: macCatalyst
2.x iOS 11 MacOS 10.15 n/a n/a macOS: macCatalyst

advantages

  • API design is easy to understand, easy to use.
  • There is no inheritance relationship, all operations are overridden based on methods, and it is less intrusive to the project.
  • On-demand registration needs to be controlledUINavigationControllerSubclass, does not affect the global appearance.
  • No changes have been made to the native navigation view hierarchy, so there is no need to worry about upgrading system compatibility.
  • Available for iOS, iPadOS, macOS, Dark Mode.
  • Support CocoaPods, Carthage, Project integration.

👏 function

Especially useful features, there is always a suitable project for you

The basic function

  • Set the navigation bar transparent
  • Set the navigation bar to translucent
  • Custom back button image
  • Custom back button
  • Custom navigation bar blur background
  • Changes the back button arrow color
  • Change the color of the navigation bar title
  • Change the background color of the navigation bar
  • Modify the background image of the navigation bar
  • Change the bottom line color of the navigation bar
  • Modify the bottom line color image of the navigation bar

Advanced features

  • Disable the swipe back gesture
  • Enable full screen swipe back gesture
  • The navigation bar returns event interception
  • Redirection to any controller
  • Navigation bar click events penetrate to the bottom
  • Dynamically change the navigation bar style
  • Updated navigation bar styles
  • Long press the Back button to display the menu function

🍽 use

All changes to the appearance of the navigation bar are made based on the UIViewController, rather than the UINavigationController, which makes the design logic more realistic. That is, the appearance of the navigation bar you are in is managed by yourself.

  1. 💉 Import header files#import <NXNavigationExtension/NXNavigationExtension.h>
  2. 💉 Before using the navigation controller, register the navigation controller to be modifiedFeatureNavigationControllerFor example:
[NXNavigationBar registerStandardAppearanceForNavigationControllerClass:[FeatureNavigationController class]].Copy the code

Note:

  • Needed to be registered before use 👉 navigation controller, navigation bar changes will only take effect after registration, the navigation controller is confined to modify registration management by the view controller, managed by the navigation controller for subclass view controller will not take effect, this framework can effectively avoid the pollution to other navigation controller, keep the principle of “who use, who registered”.
  • 🚫 Do not sign up directlyUINavigationControllerThis affects the appearance of the global navigation bar. It is recommended to create oneUINavigationControllerTo register the class.
  • 🚫 Do not use the system navigation bar hide, show method,setNavigationBarHidden:,setNavigationBarHidden:animated,setHidden:.
  • 🚫 Do not use the system navigation bar to modify transparency.
  • 🚫 Do not use the system navigation bar or navigation controllerappearanceModify related properties.
  • 🚫 do not use globaledgesForExtendedLayoutModification.
  • 🚫 Do not use<UIGestureRecognizerDelegate>Related methods disable gesture return.
  • 💉 : “Do not operate the navigation bar or navigation controller directly, leave it to themNXNavigationExtensionDeal with it.”

Tip: Don’t change something unless you understand the consequences of changing it globally. The reason for doing so is to reduce the number of detours!

🍻 Basic Functions

Changes the back button arrow color

📝 Sample code

The default color of the navigation back button is system blue[UIColor systemBlueColor]To change the color of the back button, use the following methods:

// All view controller based changes will not be overridden
NXNavigationBarAppearance.standardAppearance.tintColor = [UIColor redColor];

// Modify based on view controller
- (UIColor *)nx_barTintColor {
    return self.isDarkMode ? [UIColor whiteColor] : [UIColor blackColor];
}
Copy the code

Change the color of the navigation bar title

📝 Sample code

- (NSDictionary<NSAttributedStringKey.id> *)nx_titleTextAttributes {
    return@ {NSForegroundColorAttributeName: [self nx_barTintColor]};
}
Copy the code

Change the background color of the navigation bar

📝 Sample code

- (UIColor *)nx_navigationBarBackgroundColor {
    return [UIColor customDarkGrayColor];
}
Copy the code

Modify the background image of the navigation bar

📝 Sample code

- (UIImage *)nx_navigationBarBackgroundImage {
    return UIImage.navigationBarBackgorundImage;
}
Copy the code

Set the navigation bar transparent

📝 Sample code

- (UIColor *)nx_navigationBarBackgroundColor {
    return [UIColor clearColor];
}
Copy the code

Set the navigation bar to translucent

📝 Sample code

- (BOOL)nx_useSystemBlurNavigationBar {
    return YES;
}
Copy the code

Change the bottom line color of the navigation bar

📝 Sample code

- (UIColor *)nx_shadowImageTintColor {
    return [UIColor redColor];
}
Copy the code

Modify the bottom line color image of the navigation bar

📝 Sample code

- (UIColor *)nx_shadowImageTintColor {
    return [UIColor redColor];
}
Copy the code

Modify the bottom line color image of the navigation bar

📝 Sample code

- (UIImage *)nx_shadowImage {
    return [UIImage imageNamed:@"NavigationBarShadowImage"];
}
Copy the code

Custom back button image

📝 Sample code

- (UIImage *)nx_backImage {
    return [UIImage imageNamed:@"NavigationBarBack"];
}
Copy the code

Custom back button

📝 Sample code

- (UIView *)nx_backButtonCustomView {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@ 😋 "" forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"NavigationBarBack"] forState:UIControlStateNormal];
    [backButton setTitleColor:UIColor.customDarkGrayColor forState:UIControlStateNormal];
    return backButton;
}
Copy the code

🍺 Advanced Functions

Disable the swipe back gesture

📝 Sample code

- (BOOL)nx_disableInteractivePopGesture {
    return YES;
}
Copy the code

Enable full screen swipe back gesture

📝 Sample code

  • Locally valid (set on the page)
- (BOOL)nx_enableFullScreenInteractivePopGesture {
    return YES;
}
Copy the code
  • Globally valid (set before registering the navigation bar)
NXNavigationExtensionFullscreenPopGestureEnable = YES;
Copy the code

The navigation bar returns event interception

📝 Sample code

You need to follow the protocol

to implement the proxy method:

- (BOOL)nx_navigationController:(__kindof UINavigationController *)navigationController willPopViewController:(__kindof UIViewController *)viewController interactiveType:(NXNavigationInteractiveType)interactiveType {
    // TODO...
    return YES;
}
Copy the code
  • Intercept click back button event & gesture back event
  • Intercepts the click back button event
  • Intercept gesture return event
- (BOOL)nx_navigationController:(__kindof UINavigationController *)navigationController willPopViewController:(__kindof UIViewController *)viewController interactiveType:(NXNavigationInteractiveType)interactiveType {
    NSLog(@"interactiveType: %zd %@", interactiveType, viewController);
    
    if (self.currentItemType == EventInterceptItemTypeBackButtonAction && interactiveType == NXNavigationInteractiveTypeBackButtonAction) {
        [self showAlertControllerWithViewController:viewController];
        return NO;
    }
    
    if (self.currentItemType == EventInterceptItemTypeBackButtonMenuAction && interactiveType == NXNavigationInteractiveTypeBackButtonMenuAction) {
        [self showAlertControllerWithViewController:viewController];
        return NO;
    }
    
    if (self.currentItemType == EventInterceptItemTypePopGestureRecognizer && interactiveType == NXNavigationInteractiveTypePopGestureRecognizer) {
        [self showAlertControllerWithViewController:viewController];
        return NO;
    }
    
    if (self.currentItemType == EventInterceptItemTypeCallNXPopMethod && interactiveType == NXNavigationInteractiveTypeCallNXPopMethod) {
        [self showAlertControllerWithViewController:viewController];
        return NO;
    }
    
    if (self.currentItemType == EventInterceptItemTypeAll) {
        [self showAlertControllerWithViewController:viewController];
        return NO;
    }
    
    return YES;
}
Copy the code

Custom back button event interceptor can invoke methods: nx_popViewControllerAnimated:, nx_popToViewController: animated: Or nx_popToRootViewControllerAnimated: such as way to trigger the above agent

Redirection to any controller

📝 Sample code

  • To redirect toRandomColorViewControllerFor example, if you have pushed beforeRandomColorViewControllerIf there is no instance, it will be calledblockIf theblock == NULLorreturn nil;The redirect redirect does not occur.
  • After the redirection is performed, there is no direct jump to the corresponding view controller, if desiredjumpOperation that can be calledpopViewControllerAnimated:Return with a gesture,Click the Back button to return.
[self.navigationController nx_redirectViewControllerClass:[RandomColorViewController class] initializeStandbyViewControllerBlock:^__kindof UIViewController * _Nonnull {
    return [[RandomColorViewController alloc] init];
}];
Copy the code

Note: The above code does not jump immediately. The following code does:

[self.navigationController nx_redirectViewControllerClass:[RandomColorViewController class] initializeStandbyViewControllerBlock:^__kindof UIViewController * _Nonnull {
    return [[RandomColorViewController alloc] init];
}];
[self.navigationController popViewControllerAnimated:YES];
Copy the code

The above code looks like this: First of all, find self. NavigationController. ViewConrollers whether there is a type of [RandomColorViewController class] an instance of the object, if there is redirected to the view controller, There is no use [[RandomColorViewController alloc] init] to create a new [RandomColorViewController class] an instance of the object.

Navigation bar click events penetrate to the bottom

📝 Sample code

- (BOOL)nx_hidesNavigationBar {
    return YES;
}
Copy the code

Dynamically change the navigation bar style

📝 Sample code

- (BOOL)nx_containerViewWithoutNavigtionBar {
    return YES;
}
Copy the code

Can dynamically adjust ContainerView transparency implementation: the self nx_navigationBar. ContainerView. Alpha = value

Updated navigation bar styles

📝 Sample code

[self nx_setNeedsNavigationBarAppearanceUpdate];
Copy the code

If the status bar style has not changed, please check whether you need to call methods [self setNeedsStatusBarAppearanceUpdate], or in the subclass UINavigationController set in the following code:

- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (UIViewController *)childViewControllerForStatusBarHidden {
    return self.topViewController;
}
Copy the code

Long press the Back button to display the menu function

📝 Sample code

  • Set the backButtonMenuSupported property
if (@available(iOS 14.0, *)) {
    NXNavigationBarAppearance.standardAppearance.backButtonMenuSupported = YES;
}
Copy the code
  • In-page control
- (BOOL)nx_backButtonMenuEnabled {
    return YES;
}
Copy the code

📄 agreement

The NXNavigationExtension framework is published under the MIT license. For details, see LICENSE.

Github.com/l1Dan/NXNav…