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;
    }
  • 相关阅读:
    ★★★
    ★★
    小狼程序员:工作遐想
    广联达BB了
    计算机网络简单理解
    做个合格的(优秀的)测试开发人员
    开发、测试、测试开发
    8.21
    C++ 选择题总结(回调函数 || 类方法(实例方法)|| )
    深拷贝实现笔记
  • 原文地址:https://www.cnblogs.com/angongIT/p/4698999.html
Copyright © 2011-2022 走看看