美文网首页
iOS中在导航条设置搜索框使用小结

iOS中在导航条设置搜索框使用小结

作者: 奔跑的小蚂蚁_8b28 | 来源:发表于2021-11-11 09:10 被阅读0次
    最近在项目开发中用到了搜索框,一般是设置在列表顶部或者导航条上。下面说一下在导航条上使用搜索框的思路,刚开始是直接将CCSearchBar添加到导航条,在viewWillDisappear设置隐藏,在viewWillAppear中设置可见,结果在苹果X上出现页面卡顿的情况。原因可能是进入页面的时候获取焦点, image

    也可能是状态栏高度设置不准确,总之这个方案会有点问题。我们换另外一个思路,将导航条设置为titleview,这里需要特别注意,如果直接将CCSearchBar赋值给self.navigationItem.titleView,搜索框的长度会不受控制,需要封装一层,将搜索框添加到一个view中,然后将该view赋值给titleview。

    • (CCSearchBar *)searchBar{

      if (!_searchBar) {

        _searchBar = [[CCSearchBar alloc]init];
      
        _searchBar.backgroundColor = [UIColor clearColor];
      
        _searchBar.textfiledBgColor = K_CC_GRAY_BG_COLOR;
      
        _searchBar.placeholder = K_CC_LOCAL_STR(@"home.search");
      
        _searchBar.placeHolderFontSize = 14;
      
        _searchBar.placeHolderTextColor = K_CC_TEXT_GRAY_COLOR;
      
        _searchBar.textPosition = SearchBarTextPositionCenter;
      
        _searchBar.textColor = K_CC_COLOR_STRING(@"17315C");
      
        _searchBar.frame = CGRectMake(0, 5, K_CC_SCREEN_WIDTH-146, 34);
      
        _searchBar.delegate = self;
      

      }

      return _searchBar;

    }

    • (UIView *)toolSearch

    {

    if (!_toolSearch) {
    
        _toolSearch=[[UIView alloc]initWithFrame:CGRectMake(0, 0, K_CC_SCREEN_WIDTH - 106, 44)];
    
        _toolSearch.backgroundColor =  [UIColor clearColor];
    
        [_toolSearch addSubview:self.searchBar];
    
    }
    
    return _toolSearch;
    

    }

    self.navigationItem.titleView=self.toolSearch;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        [self.searchBar becomeFirstResponder];
    
    });
    

    相关文章

      网友评论

          本文标题:iOS中在导航条设置搜索框使用小结

          本文链接:https://www.haomeiwen.com/subject/wmuwzltx.html