美团HD(6)-添加搜索遮罩

DJSelectCityViewController.m

/** SearchBar开始编辑 */
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

    // 隐藏导航栏
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    // 显示遮罩
    UIView *cover = [[UIView alloc] init];
    cover.backgroundColor = [UIColor blackColor];
    cover.alpha = 0.2;
    cover.frame = self.cityTableView.frame;
    cover.tag = DJCoverTag;
    
    // 由于UIView 不是UIControl,所以没有addTarget方法,可以使用UITapGestureRecognizer代替
    // 当conver被点击时,移除第一响应者
    [cover addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:searchBar action:@selector(resignFirstResponder)]];
    
    [self.view addSubview:cover];

}


/** SearchBar结束编辑 */
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {

    // 显示导航栏
    [self.navigationController setNavigationBarHidden:NO animated:YES];

    // 隐藏遮罩
    [[self.view viewWithTag:DJCoverTag] removeFromSuperview];
    
}

最终效果:

原文地址:https://www.cnblogs.com/yongdaimi/p/6270597.html