zoukankan      html  css  js  c++  java
  • iOS开发之UISearchBar初探

    iOS开发之UISearchBar初探

     

    UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle、text、placeholder等等。但是这些属性显然不足矣满足我们的开发需求。比如:修改placeholder的颜色、修改UISearchBar上面的UITextfield的背景颜色、修改UITextfield上面的照片等等。

    为了实现上述的需求,最好写一个UISearchBar的子类就叫LSSearchBar吧

    LSSearchBar.h如下:

    #import <UIKit/UIKit.h>
    
    @interface LSSearchBar : UISearchBar
    
    @end
    

    LSSearchBar.m如下:

    #import "LSSearchBar.h"
    
    @implementation LSSearchBar
    
    - (void)layoutSubviews {
    
        [super layoutSubviews];
    
        //通过遍历self.subviews找到searchField
        UITextField *searchField;
        NSUInteger numViews = [self.subviews count];
        for(int i = 0; i < numViews; i++) {
            if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
                searchField = [self.subviews objectAtIndex:i];
            }
        }
    
        //如果上述方法找不到searchField,那就试试下面的方法吧
    
        if (searchField ==  nil) {
            NSArray *arraySub = [self subviews];
            UIView *viewSelf = [arraySub objectAtIndex:0];
            NSArray *arrayView = [viewSelf subviews];
            for(int i = 0; i < arrayView.count; i++) {
                if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {
                    searchField = [arrayView objectAtIndex:i];
                }
            }
        }
    
    
        if(!(searchField == nil)) {
            //设置颜色
            searchField.textColor = [UIColor whiteColor];
    
            //设置背景颜色
            [searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
            [searchField setBorderStyle:UITextBorderStyleNone];
    
            //设置placeholder的颜色
            [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    
            //设置searchField上的照片
            UIImage *image = [UIImage imageNamed:@"search"];
            UIImageView *iView = [[UIImageView alloc] initWithImage:image];
            iView.frame = CGRectMake(0, 0, 15, 15);
            searchField.leftView = iView;
        }
    
    }
    
    @end
    

     

    本文出处刚刚在线:http://www.superqq.com/blog/2015/01/19/ioskai-fa-zhi-uisearchbarchu-tan/

  • 相关阅读:
    第一次结对编程作业
    第一次个人编程作业
    获取file中字段,写入到TXT文件中
    通过file中的字段查询MySQL内容
    MySQL常用语句
    MySQL乱码问题
    脚本数据编码格式转换
    mysql 常用命令操作
    thinkphp项目 Class 'finfo' not found
    POJ3255--次短路
  • 原文地址:https://www.cnblogs.com/easyToCode/p/5196905.html
Copyright © 2011-2022 走看看