Implementation of IOS custom UISearchBar and classification

Time:2022-6-1

preface

The company decided to upgrade the interface and unify the UI. Because the UISearchBar of the system is too ugly, it can only be customized

Implement dhsearchbarview

Customize dhsearchbarview. The interface is as follows

Implementation of IOS custom UISearchBar and classification

image.png

Obviously, there are searchbarview, searchimageview, searchtextfield

Here are the two block callbacks of dhsearchbarview. Considering that there will be time-to-time search and click the retry key in the requirements, there are two callback codes as follows

@property (nonatomic, strong) void (^textFieldDidChangeCallBack)(DHSearchBarView *searchBarView, NSString *content);

@property (nonatomic, strong) void (^textFieldReturnCallBack)(DHSearchBarView *searchBarView, NSString *content);

Implementation classification

Load directly in the controller in the Xib mode, which is no problem
But considering that many places in our project will use the search box
Therefore, the classification method can be used
Add the classification uiviewcontroller+searchbarview to the controller. The code is as follows
. Code in H

@interface UIViewController (SearchBarView)

@property (nonatomic, strong, nullable) DHSearchBarView *dh_searchBarView;

- (DHSearchBarView *)addSearchBarViewWithPlaceHolder:(NSString * __nullable)placeHolder;

@end

. Code in M

static NSString *searchBarViewKey = @"DHSearchBarView";

@implementation UIViewController (SearchBarView)

- (DHSearchBarView *)addSearchBarViewWithPlaceHolder:(NSString * __nullable)placeHolder
{
    if (!self.dh_searchBarView) {
        self.dh_searchBarView = [DHSearchBarView allocInitFormXib];
    }
    
    self.dh_searchBarView.searchTextField.placeholder = placeHolder;

    [self.view addSubview:self.dh_searchBarView];
    
    [self.dh_searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
          
        make.left.right.top.mas_equalTo(self.view);
        make.height.mas_equalTo(@(40));
    }];
    
    return self.dh_searchBarView;
}

#pragma mark - getter && setter

- (DHSearchBarView *)dh_searchBarView
{
    return objc_getAssociatedObject(self, &searchBarViewKey);
}

- (void)setDh_searchBarView:(DHSearchBarView *)dh_searchBarView
{
    objc_setAssociatedObject(self, &searchBarViewKey, dh_searchBarView, OBJC_ASSOCIATION_RETAIN);
}

@end

analysis

  1. . Create entity class DH in H_ searchBarView
    Add DH to the name_ To prevent duplicate names with those in the controller
  2. . Implementation of set get method in M
    Using objc in the rumtime mechanism_ Setassociatedobject and objc_ Getassociatedobject implementation
  3. . Add method addsearchbarviewwithplaceholder in M
    When used, you can return the created dhsearchbarview to facilitate data processing in the contributor, or modify some styles of the control

apply

The code is as follows

kWeakself
    
    Dhsearchbarview *searchbarview = [self addsearchbarviewwithplaceholder:@ "please enter a keyword or number to search"];
    
    searchBarView.textFieldDidChangeCallBack = ^(DHSearchBarView * _Nonnull searchBarView, NSString * _Nonnull content) {
        
        weakself.contract_name = content;
        
        weakself.pageIndex = 1;
        [weakself loadDataWithAnimation:NO text:weakself.contract_name];
    };
}

ponder over

1. it is easier to use the classification. As long as the project references the classification, the function can be realized by adding a sentence of code
2. classification can make the code simpler and smoother

2021.7.7

The uiviewcontroller+searchbarview classification code can be directly added to dhsearchbarview, which is more simple and convenient

Recommended Today

C # multithreading method for processing multiple data

catalogue summary code implementation Code address summary Multithreading refers to the technology of realizing the concurrent execution of multiple threads from software or hardware. Computers with multithreading capability can execute more than one thread at the same time due to hardware support, thereby improving the overall processing performance. Systems with this capability include symmetric multiprocessors, […]