“This is the 17th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

Requirement: The same module subject requirement is consistent implementation: unified control style through custom navigation bar

  • Set the gradient color of the navigation bar
  • Sets the global navigation bar button theme
  • Intercepting push: Override the native method implementation using a custom class

I Custom navigation bar

1.1 Customizing navigation Bars

#import <UIKit/UIKit.h>
#import "UIBarButtonItem+Extension.h"
#import "ImageTools.h"

@interface HWNavigationController : UINavigationController

+ (void) setttingAppearance;

- (void)setupNavigationBarBarStyle:(NSInteger)barStyle;
#pragmaMark-******** sets the style of the list controller
+ (void)setupListnavigationItemAndBarStyle:(UIViewController*)vc;
+ (void)setupDetailnavigationItemAndBarStyle:(UIViewController*)vc;
@end

Copy the code

1.2 Setting a button theme for the Global Navigation Bar

setttingAppearance

#import "HWNavigationController.h"
#import "GYQBaseBarItem.h"
@implementation HWNavigationController
    
    

#pragmaMark - Sets the navigation bar theme


+ (void) setttingAppearance{
    // Set the global navigation bar appearance
    [self settingUINavigationBarAppearance];
    if (IOS7) {
        return;// There is no need to set the global navigation bar button theme
    }
    // Set the global navigation bar button theme
    [self settingbarButtonItenAppearance];
}


Copy the code

settingUINavigationBarAppearance

+ (void) settingUINavigationBarAppearance{
    /* @protocol UIAppearance 
      
        protocol proxy method + (instanceType)appearance; @interface UIView : UIResponder < UIAppearance> */
      
    // Method 1: Get the global appearance
    // UINavigationBar *navigationBar =[UINavigationBar appearance]; // Get the appearance of all navigation bars
    // Method 2: Get the navigation bar of our own navigation controller -- make sure that the navigation bar of other functions of the system (SMS) conflicts with ours, especially in the area of SMS sharing
    UINavigationBar *navigationBar;
    if (IOS9) {
        / / 9.0 API
        navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[HWNavigationController class]]];
    }else{
        navigationBar = [UINavigationBar appearanceWhenContainedIn:[HWNavigationController class].nil];
    }
    
    /** the layout of navigation background in iOS6 is not retina: 320x44 px retina: 640x88 px */
    
    
    
    [self setupUINavigationBarsetBackgroundImage:navigationBar];
    /*2. *title: @property(nonatomic,copy) NSDictionary *titleTextAttributes; // The latest version of the key is in the NSAttributedString.h UIKit framework */
    // NSDictionary *dict = @{UITextAttributeTextColor:[UIColor whiteColor]};
    NSDictionary *dict = @{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:kNavListNSFontAttributeName};
    [navigationBar setTitleTextAttributes:dict];
    //2, The tint color to apply to The navigation items and bar button items. The theme color of the navigation bar
    [navigationBar setTintColor:[UIColor whiteColor]];
    
    
}

Copy the code

1.3 Setting the gradient color of the navigation bar

setupUINavigationBarsetBackgroundImage

+ (void)setupUINavigationBarsetBackgroundImage:(UINavigationBar*)navigationBar{
    
    UIImage *bgImg = [UIImage getMaingradientColorImage];
    [navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];

    
}



Copy the code

1.4 Intercept push


#pragmaMark-intercept push; --//90% of interceptions are implemented using custom classes that override their own methods
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count>0) {[self setNavigationBarHidden:NO animated:NO];
        viewController.hidesBottomBarWhenPushed =YES;
        

        
        if ([viewController respondsToSelector:@selector(KNbackAction)]) {
            
            UIBarButtonItem *tmp =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:QCTNAVicon_left] style:0 target:viewController action:@selector(KNbackAction)];

            viewController.navigationItem.leftBarButtonItem = tmp;

        }else{
            
            UIBarButtonItem *tmp =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:QCTNAVicon_left] style:0 target:self action:@selector(backAction)]; viewController.navigationItem.leftBarButtonItem = tmp; }} [super pushViewController:viewController animated:animated];

}
#pragmaMark - rewritten: Animated:
/** 1) The value of custom navigation controller overwrites the push method to intercept all child controllers pushed on the stack and do some processing */
    
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void(^) (void))completion{
    
    
    
// if ([[viewControllerToPresent topViewController] respondsToSelector:@selector(KNprebackAction)]) {

        
        if ( [viewControllerToPresent  isKindOfClass:[UINavigationController class]]) {
            
            
            
            
            

            
            
            UINavigationController  *viewControllerToPresenttmp = (UINavigationController*)viewControllerToPresent;
            
            weakSelf(weakSelf);

            if ([[viewControllerToPresenttmp topViewController] respondsToSelector:@selector(KNprebackAction)]) {
                
                GYQBaseBarItem *tmp =[[GYQBaseBarItem alloc]initWithImage:[UIImage imageNamed:QCTNAVicon_left] style:0 target:[viewControllerToPresenttmp topViewController] action:@selector(KNprebackAction)];
                tmp.baseVC = viewControllerToPresent;
                viewControllerToPresenttmp.topViewController.navigationItem.leftBarButtonItem = tmp;

                
                
            }else{
                
                GYQBaseBarItem *tmp =[[GYQBaseBarItem alloc]initWithImage:[UIImage imageNamed:QCTNAVicon_left] style:0 target:weakSelf action:@selector(presenbackAction:)]; tmp.baseVC = viewControllerToPresent; viewControllerToPresenttmp.topViewController.navigationItem.leftBarButtonItem = tmp; }} [superpresentViewController:viewControllerToPresent animated:flag completion:completion]; } - (void)presenbackAction:(GYQBaseBarItem *)bar{
    
    // Once you click back, the initial process is interrupted
    [bar.baseVC dismissViewControllerAnimated:YES completion:nil];
}
    
- (void)backAction{
    
    [self popViewControllerAnimated:YES];

    
    
    
}

Copy the code

1.5 black edge below navigation bar after transparency is removed (for iOS15)

[self.navigationBar setShadowImage:[UIImage alloc] init]];

After iOS15


    if(@available(iOS 15.0, *)) {
        UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
        
        // Remove the black edge below the transparent navigation bar
        appearance.shadowImage =[[UIImage alloc] init];
        
        appearance.shadowColor= UIColor.clearColor;


        
        navigationBar.standardAppearance = appearance;
        
        navigationBar.scrollEdgeAppearance = appearance;
        
    }



Copy the code

II Related Codes

The macro

#defineKAdjustRatio (num) (ceil ((SCREENW / 375.0) * (num)))

#define kPingFangFont(fontSize) [UIFont fontWithName:@"PingFang-SC-Medium" size:(kAdjustRatio(fontSize))]

#define kTextFont(size) [UIFont systemFontOfSize:(kAdjustRatio(size))]


#define kPingFangHeavyFont(fontSize) [UIFont fontWithName:@"PingFang-SC-Heavy" size:(kAdjustRatio(fontSize))]

#define kPingFangNOkAdjustRatioFont(fontSize) [UIFont fontWithName:@"PingFang-SC-Medium" size:((fontSize))]



#define kBoldFont(fontSize) [UIFont fontWithName:@"Helvetica-Bold" size:(kAdjustRatio(fontSize))]


Copy the code
  • Retrieves the searchField of the searchBar

 UITextField *searchField = [searchBar valueForKey:@"searchField"];
    
    [searchField setBackgroundColor:HWColor(245.245.245)];
Copy the code

Get navigationBar

if(IOS9) {//9.0 API navigationBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[GYQNavigationControllerViewController class]]]; [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]]; }else{
        navigationBar = [UINavigationBar appearanceWhenContainedIn:[GYQNavigationControllerViewController class],nil];
        
        [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];

    }
    [navigationBar setShadowImage:[ImageTools createImageWithColor: QBW221Color]];


Copy the code

see also

For more, check out # Applets: iOS Reverse, which presents valuable information only for you, focusing on the mobile technology research field.