zoukankan      html  css  js  c++  java
  • 新浪微博客户端(5)-自定义UISearchBar

    iOS自带的UISearchBar有很多限制,我们可以使用UITextField做出一个类似于SearchBar的效果。

    //=================================================
        //                 自定义SearchBar
        //=================================================
        
        // 1.创建一个UITextField作为背景
        UITextField *searchBar = [[UITextField alloc] init];
        searchBar.width = 420;
        searchBar.height = 30;
        searchBar.font = [UIFont systemFontOfSize:14];
        searchBar.background = [UIImage imageNamed:@"searchbar_textfield_background"];
    
        // 2.添加左侧的小图标
    //    UIImage *searchIcon = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    //    UIImageView *searchIconView = [[UIImageView alloc] initWithImage:searchIcon]; // 使用此种形式创建出来的ImageView是有默认大小的。
        
        UIImageView *searchIconView = [[UIImageView alloc] init];
        searchIconView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
        searchIconView.width = 30;
        searchIconView.height = 30;
        
        searchIconView.contentMode = UIViewContentModeCenter;
        searchBar.leftView = searchIconView;
        searchBar.leftViewMode = UITextFieldViewModeAlways;
        
        self.navigationItem.titleView = searchBar;

    最终效果:

  • 相关阅读:
    严格模式
    es6模块与 commonJS规范的区别
    Javascript内置对象、原生对象、宿主对象关系
    实现继承的几种方式
    创建对象的一些方式
    null的小扩展
    getElementById的缩略
    你真的知道为什么不推荐使用@import?
    换行与不换行
    transition与animation
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/5967662.html
Copyright © 2011-2022 走看看