UISearchBar(圆角、背景)


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]init];
    [tapGest setNumberOfTapsRequired:1];
    [tapGest setNumberOfTouchesRequired:1];
    [tapGest addTarget:self action:@selector(tapGesut)];
    
    [self.view addGestureRecognizer:tapGest];
    
    search_Bar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 50, 320, 40)];
    search_Bar.delegate = self;
//    search_Bar.alpha = .5f;
    /**
     *  搜索框风格
     *  UISearchBarIconSearch/UISearchBarStyleProminent = UISearchBarStyleDefault  放大镜在中间,默认风格
     *  UISearchBarStyleMinimal  最小风格,没外层边框
     *
     */
    search_Bar.searchBarStyle = UISearchBarStyleMinimal;
    
    /**
     *  搜索框外层颜色
     */
    search_Bar.barTintColor = [UIColor lightGrayColor];
    
    
    /**
     *  光标、取消按钮颜色
     */
    search_Bar.tintColor = [UIColor redColor];
    

    /**
     *  居右的提示,输入后文字会消失
     */
    search_Bar.placeholder = @"2121";
    
    
    /**
     *  半透明
     *  ios 6默认是NO,但7中YES也没效果,要设置alphe 透明才可以
     */
    search_Bar.translucent = YES;
    
    
    /**
     * 键盘最上方的视图,你妹,找到宝了,这是加入到
     */
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    view.backgroundColor = [UIColor redColor];
    search_Bar.inputAccessoryView = view;
    

    /**
     *  改变按钮图片,正常在选中方法里面处理,如果在这里处理,直接平铺到搜索框内
     */
//  [search_Bar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];

    
    [self.view addSubview:search_Bar];

    
//____________未核心功能_____________

     //搜索框内容
     //如果一开妈有值,程输入状态
//   search_Bar.text = @" ";
    

     //居中的提示文字,不会因为输入内容消失
//   search_Bar.prompt = @"12e";
    
    //
    [search_Bar setShowsScopeBar:YES];

    //跟searchBarStyle 感觉没什么用
//    search_Bar.barStyle = UIBarPositionTop;
    
    //直接setBackgroundImage效果一样,前提是 UISearchBarStyleMinimal 无外框类型
//    search_Bar.scopeBarBackgroundImage = [UIImage imageNamed:@"1"];

    //类似选择器,还不知道有什么用
//    search_Bar.scopeButtonTitles = @[@"2",@"3",@"5"];
    
    
    
}

//开始选中搜索框
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    //显示在框外右侧 取消-按钮
//  [searchBar setShowsCancelButton:YES animated:YES];
    
    //显示在框内右侧 书-按钮
  [search_Bar setShowsBookmarkButton:YES];
  [search_Bar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
    //显示在框内右侧 下拉箭头-按钮
//  [search_Bar setShowsSearchResultsButton:YES];
    
    
    return YES;
}

//点击取消-按钮还原搜索条大小
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [search_Bar resignFirstResponder];
    [search_Bar setShowsCancelButton:NO animated:YES];
    
}

//点击书-按钮还原搜索条大小
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
    
    //选中改变图片
    [search_Bar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];

}

//点击下拉箭头-按钮还原搜索条大小
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    [searchBar setSearchResultsButtonSelected:!searchBar.searchResultsButtonSelected];
}


//监测搜索框文字输入时执行
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //实时获取搜索框内 信息
    
}

//点击键盘"搜索"执行
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
}



//空白处理添加点击手势 收起键盘
-(void)tapGesut
{
    [search_Bar resignFirstResponder];
    [search_Bar setShowsCancelButton:NO animated:YES];
}




//当输入完 点击屏幕空白处认为是 不再输入,执行此方法
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    //    [search_Bar resignFirstResponder];
    //    [search_Bar setShowsCancelButton:NO animated:YES];
}


http://www.cnblogs.com/-ios/p/5948643.html

/** 取消searchBar背景色 */

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size

{

    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

      return image;

}

 

然后在viewdidload里面

/** 设置背景图片 */

    [_searchBar setBackgroundImage:[UIImage imageNamed:@"red_nav_bar"]];   //这里图片的颜色和导航栏一样

/** 设置背景色 */

   [_searchBar setBackgroundColor:[UIColor redColor]];

/** 设置文本框背景 * 

[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"imageView"] forState:UIControlStateNormal];

 

 

    //更改search圆角

        UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

        if (searchField) {

            [searchField setBackgroundColor:[UIColor whiteColor]];

            searchField.layer.cornerRadius = 14.0f;

            searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;

            searchField.layer.borderWidth = 1;

            searchField.layer.masksToBounds = YES;

        }


更改UISearchBar占位符居左

http://www.jianshu.com/p/0787277973d4

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值