iOS13 UISearchController + MJRefresh

最近iOS的客戶端適配iOS13,需要將原來(lái)的UISearchDisplayController替換為UISearchController,加上頁(yè)面中存在UITableView + MJRefresh的結(jié)構(gòu),實(shí)際調(diào)整中出現(xiàn)了各種細(xì)節(jié)問(wèn)題,去查資料,現(xiàn)在iOS這生態(tài)...確實(shí)沒(méi)查到什么資料...全靠自己瞎幾把調(diào),所以為了方便遇到同樣問(wèn)題的同行,避免浪費(fèi)無(wú)謂的時(shí)間,就把適配后的代碼放出來(lái),且看代碼:

@interface YourViewController ()< UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>

/** 搜索vc */
@property (nonatomic, strong) UISearchController *searchController;

/** 聯(lián)系人列表 */
@property (nonatomic, strong) UITableView *contactTableView;

/** 搜索內(nèi)容 */
@property (nonatomic, strong) NSString *searchContent;

/** 正常狀態(tài)數(shù)據(jù)源 */
@property (nonatomic, strong) NSMutableArray *contactArr;

/** 搜索狀態(tài)數(shù)據(jù)源 */
@property (nonatomic, strong) NSMutableArray *searchedContactArr;

@end

@implementation YourViewController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    //初始化ui
    [self initView];
}

#pragma mark - tableview DataSource / delegate

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
    if (self.searchController.isActive && self.searchContent.length > 0) {
        //處于搜索狀態(tài)且存在搜索內(nèi)容時(shí)
    
    } else {
       //正常狀態(tài)
   
    }
}


//....省略若干其他無(wú)關(guān)tableview的代理方法,這些代理方法中區(qū)分是否搜索狀態(tài)的處理邏輯都同上numberOfSectionsInTableView方法


- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    //重寫(xiě)這個(gè)代理方法,是為了在存在搜索結(jié)果時(shí),滑動(dòng)列表收起鍵盤(pán)
    [self.searchController.searchBar resignFirstResponder];
}



#pragma mark - searchBar delegate
/**
 *  點(diǎn)擊cancle按鈕時(shí)調(diào)用
 */
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    //點(diǎn)擊取消按鈕后,清空已有搜索結(jié)果
    self.searchContent = @"";
    //清空搜索結(jié)果列表
    [self.searchedContactArr removeAllObjects];
    //刷新tableview
    [self.contactTableView reloadData];
}



#pragma mark - UISearchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)aSearchController {
    //停止刷新?tīng)顟B(tài)
    [self.contactTableView.mj_header endRefreshing];

    if (self.searchController.isActive) {
        //搜索狀態(tài)禁止刷新
        self.contactTableView.mj_header.hidden = YES;
    } else {
        //正常狀態(tài)允許刷新
        self.contactTableView.mj_header.hidden = NO;
    }

    self.searchContent = aSearchController.searchBar.text;
    //調(diào)用搜索方法
    [self getSearchedContactList];
    //得到搜索結(jié)果后,刷新UI
    [self.contactTableView reloadData];
}


 #pragma mark - 私有方法

- (void)initView
{
    [self.contactTableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.and.right.equalTo(self.view);
    make.top.equalTo(self.mas_topLayoutGuide);
    make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];


    //關(guān)于UISearchController的所有設(shè)置
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    //設(shè)置代理
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
    //進(jìn)入搜索狀態(tài)是否隱藏導(dǎo)航欄,yes為隱藏,no為不隱藏;默認(rèn)是yes
    self.searchController.hidesNavigationBarDuringPresentation = YES;
    //這個(gè)屬性忘了啥意思,反正有用
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //設(shè)置下邊這個(gè)屬性是為了返回到前一個(gè)頁(yè)面,使searchBar消失
    self.definesPresentationContext = YES;
    //設(shè)置searchbar相關(guān)
    self.searchController.searchBar.placeholder = @"搜索聯(lián)系人";
    [self.searchController.searchBar setBackgroundColor:[UIColor whiteColor]];
    self.searchController.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.searchController.searchBar.tintColor = SC_COLOR(kColorStressClickable);
    self.searchController.searchBar.delegate = self;
    //隱藏searchBar的分割線
    self.searchController.searchBar.backgroundImage = [[UIImage alloc] init];
    self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 56);
    [self.searchController.searchBar sizeToFit];
    //設(shè)置searchBar中的UItextfield
    UITextField *searchBarTextField = [[UITextField alloc] init];
    if (@available(iOS 13.0, *)) {
        searchBarTextField = self.searchController.searchBar.searchTextField;
        searchBarTextField.backgroundColor = [UIColor whiteColor];
    } else {
        searchBarTextField = [self.searchController.searchBar valueForKey:@"_searchField"];
    
        //設(shè)置searchbar的背景色和文本框顏色
        for(UIView *view in [[[self.searchController.searchBar subviews] objectAtIndex:0] subviews]){
            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                view.alpha = 0.0f;
        } else if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
                view.backgroundColor = [UIColor whiteColor];
            }
        }
    }
    [searchBarTextField setFont:[UIFont systemFontOfSize:14]];
    searchBarTextField.layer.cornerRadius = 8.0f;//設(shè)置圓角具體根據(jù)實(shí)際情況來(lái)設(shè)置
    searchBarTextField.textColor = SC_COLOR(kColorNormalDividerBlockText); //字體顏色
    searchBarTextField.backgroundColor = SC_COLOR(kColorBgLightLightGray); //背景顏色

    //下拉刷新
    self.contactTableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.contactTableView.mj_header.automaticallyChangeAlpha = YES;

    self.contactTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //設(shè)置列表頭部為搜索框
    self.contactTableView.tableHeaderView = self.searchController.searchBar; 
    }


#pragma -mark 懶加載

- (UITableView *)contactTableView
 {
    if (!_contactTableView) {
        _contactTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self.view addSubview:_contactTableView];
        _contactTableView.dataSource = self;
        _contactTableView.delegate = self;
    
        [_contactTableView registerClass:[YourTableViewCell class] forCellReuseIdentifier:cellIndentify];
    
      //這里我也忘了,不懂自己百度吧
        if (@available(iOS 11.0, *)) {
            _contactTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _contactTableView;
}

@end

以上關(guān)于搜索框的處理的前提是,搜索結(jié)果頁(yè)面和正常展示的頁(yè)面共用同一套tableview,其他的就不解釋了,注釋都有...

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容