zoukankan      html  css  js  c++  java
  • 自定义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;
    }
  • 相关阅读:
    通过实例来分析I2C基本通信协议
    POJ 1470 Closest Common Ancestors【近期公共祖先LCA】
    并发 错误 java.lang.IllegalMonitorStateException: current thread not owner 分析
    【Nginx】HTTP请求的11个处理阶段
    JavaScript获取当前值
    air游戏接入小米支付sdk
    [学习笔记]Pollard-Rho
    2018-2-13-win10-UWP-Markdown-含源代码
    2018-2-13-win10-UWP-Markdown-含源代码
    2018-2-13-win10-uwp-图标制作器
  • 原文地址:https://www.cnblogs.com/angongIT/p/4698999.html
Copyright © 2011-2022 走看看