zoukankan      html  css  js  c++  java
  • iOS中在导航条设置搜索框使用小结

      最近在项目开发中用到了搜索框,一般是设置在列表顶部或者导航条上。下面说一下在导航条上使用搜索框的思路,刚开始是直接将CCSearchBar添加到导航条,在viewWillDisappear设置隐藏,在viewWillAppear中设置可见,结果在苹果X上出现页面卡顿的情况。原因可能是进入页面的时候获取焦点,

     

    也可能是状态栏高度设置不准确,总之这个方案会有点问题。我们换另外一个思路,将导航条设置为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];

        });

  • 相关阅读:
    基于Karma和Jasmine的angular自动化单元测试
    【转】使用SVG中的Symbol元素制作Icon
    【转】整理分析:Before 和 :After及其实例
    【转载】CSS中强大的EM
    【转】提升说服力!UI设计的心理学
    解决IE8不支持数组的indexOf方法
    KISSY Slide 组件应用遇到的一个放大缩小问题
    jQuery.extend 函数详解(转载)
    事件冒泡分析及return false、preventDefault、stopPropagation的区别
    jquery学习(一)-选择器
  • 原文地址:https://www.cnblogs.com/bigant9527/p/15538420.html
Copyright © 2011-2022 走看看