Clean up the project today, add comments to the project, and find some things that need to be saved for future use.

  • Set the background of navBar, remove the black line, tried for a long time, looked up a lot, this works for me
  • Sets the color of the navBar item, as well as the color and font size of the Nav Title
  • To replace the picture of the system return button, the design said that the built-in one was too ugly. At first, it was to customize the returned View, but later I found that there was a direct way to replace it without so much trouble
// Set the navBar background so that the black line is removed

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_bar"] forBarMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

[[UINavigationBar appearance] setTranslucent:NO];

    

// Set navBar button tintColor, title font size and color

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName: [MKCommonData commonNavigationBarFontType] size: 20.0], NSForegroundColorAttributeName: [UIColor whiteColor]}];

    

// Set the background image of the navBar return button

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"icon_white"]];

[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"icon_white"]];



// Remove the text for the return button

 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

Copy the code

After the return button is customized, the system’s side slide return will be invalid. Before, I always set each interface, open and close, but later I found that I can directly set all of them

self.navigationController.interactivePopGestureRecognizer.delegate = self; // Slide back, customize the return button to take effect, the top Settings can be pushed out of the interface



#pragma mark - gestureRecognizer delegate - 

// Slide back, if the home page is disabled, not the home page is enabled

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if (self.navigationController.viewControllers.count == 1) {

        return NO;

    } else {

        return YES;

    }

}

Copy the code

This is a GCD countdown, send verification code implementation

#pragma mark - GCD implements countdown

- (void)countDown

{

__block int timeout = 61; // Countdown time

Self. countdownlabel. text = [NSString stringWithFormat:@" %d seconds ", timeout];

    self.countDownLabel.textColor = [UIColor lightGrayColor];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    timer = dispatch_source_create((DISPATCH_SOURCE_TYPE_TIMER), 0, 0, queue);

Dispatch_source_set_timer (timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); // execute every second

    dispatch_source_set_event_handler(timer, ^{

        timeout--;

        if (timeout <= 0) {

// The timer is closed

            dispatch_source_cancel(timer);

            dispatch_async(dispatch_get_main_queue(), ^{

/ / end

                

            });

        } else {

NSString * strTime = [NSString stringWithFormat:@" Take %d seconds ", timeout];

            dispatch_async(dispatch_get_main_queue(), ^{

// Update the timeLabel display

                self.countDownLabel.text = strTime;

            });

        }

    });

    dispatch_resume(timer);

}

Copy the code

SearchBar’s built-in cancel button is Cancel in English, but the product forced it to be in Chinese, so I had to change it

// Change the text of the cancel button when searchBar starts editing

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

    _searchBarView.searchBar.showsCancelButton = YES;

    NSArray *subViews;

    CGFloat deviceVersion = [UIDevice currentDevice].systemVersion.floatValue;

If (deviceVersion >= 7.0) {

        subViews = [_searchBarView.searchBar.subviews[0] subviews];

    } else {

        subViews = _searchBarView.searchBar.subviews;

    }

    

    for (id view in subViews) {

        if ([view isKindOfClass:[UIButton class]]) {

            UIButton *cancelButton = (UIButton *)view;

[cancelButton setTitle: @ "cancel" forState: UIControlStateNormal];

            break;

        }

    }

}

Copy the code

The request is in json string format. Before, I always thought the parameter was a dictionary type. Later, I checked it carefully and found that it was id. Direct dictionary; B. Change the dic in the parameter to a JSON string, and then assign the string as a value to a key. C. a json string. Say too much is tears.

// Convert the parameter to json string

- (NSString *)convertJSONStringWithObject:(id)jsonObject

{

    NSData *jsonData;

    if ([NSJSONSerialization isValidJSONObject:jsonObject]) {

        NSError *error;

        jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject

                                                   options:NSJSONWritingPrettyPrinted

                                                     error:&error];

        if (error) {

            return nil;

        }

    }

    else {

        jsonData = jsonObject;

    }

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    

    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];

    NSRange range = {0, jsonString.length};

    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

    NSRange range2 = {0, mutStr.length};

    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

    return [NSString stringWithFormat:@"%@", mutStr];

} ` ` `

Copy the code