• 自定义searchbar,修改背景色


    1、自定义searchBar:

         (1).h:

    #import <UIKit/UIKit.h>
    
    @interface EMSearchBar : UISearchBar
    
    /**
     *  自定义控件自带的取消按钮的文字(默认为“取消”/“Cancel”)
     *
     *  @param title 自定义文字
     */
    - (void)setCancelButtonTitle:(NSString *)title;
    
    @end

      (2).m:

    #import "EMSearchBar.h"
    
    @implementation EMSearchBar
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            for (UIView *subView in self.subviews) {
                if ([subView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                    [subView removeFromSuperview];
                }
                
                if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
                    UITextField *textField = (UITextField *)subView;
                    [textField setBorderStyle:UITextBorderStyleNone];
                    textField.background = nil;
                    textField.frame = CGRectMake(8, 8, self.bounds.size.width - 2* 8,
                                                 self.bounds.size.height - 2* 8);
                    textField.layer.cornerRadius = 6;
                    
                    textField.clipsToBounds = YES;
                    textField.backgroundColor = [UIColor whiteColor];
                }
            }
        }
        return self;
    }
    
    /**
     *  自定义控件自带的取消按钮的文字(默认为“取消”/“Cancel”)
     *
     *  @param title 自定义文字
     */
    - (void)setCancelButtonTitle:(NSString *)title
    {
        for (UIView *searchbuttons in self.subviews)
        {
            if ([searchbuttons isKindOfClass:[UIButton class]])
            {
                UIButton *cancelButton = (UIButton*)searchbuttons;
                [cancelButton setTitle:title forState:UIControlStateNormal];
                break;
            }
        }
    }
    
    @end

    2、修改背景色

    - (UISearchBar *)searchBar
    {
        if (!_searchBar) {
            _searchBar = [[EMSearchBar alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 44)];
            _searchBar.delegate = self;
            _searchBar.placeholder = NSLocalizedString(@"search", @"Search");
            //        _searchBar.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.000];
            _searchBar.backgroundImage = [self imageWithColor:[UIColor clearColor]];
        }
        
        return _searchBar;
    }
    
    - (UIImage *)imageWithColor:(UIColor *)color
    {
        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [color CGColor]);
        CGContextFillRect(context, rect);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
  • 相关阅读:
    java数组------数组基本使用和3中初始化方式
    java面向对象-------final关键字
    java面向对象------- 多态
    java面向对象------- 封装
    Android 音视频开发(五):使用 MediaExtractor 和 MediaMuxer API 解析和封装 mp4 文件
    Android 音视频开发(四):使用 Camera API 采集视频数据
    音频 PCM 数据的采集和播放
    http协议的学习
    Kotlin入门学习笔记
    RxJava笔记
  • 原文地址:https://www.cnblogs.com/angongIT/p/4698999.html
Copyright © 2020-2023  润新知