美文网首页iOS
SearchBar自定义

SearchBar自定义

作者: 追逐你的影子 | 来源:发表于2017-05-29 12:50 被阅读0次
        UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 260, self.view.frame.size.width - 16, 35)];
        searchBar.searchBarStyle = UISearchBarStyleMinimal;
        searchBar.placeholder = @"请输入关键字";
        [searchBar setImage:[UIImage imageNamed:@"test_sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; //替换收缩放大镜
        [searchBar setImage:[UIImage imageNamed:@"test_quxiao"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal]; //输入的时候,右侧的清除按钮
        [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"sousuokuang"] forState:UIControlStateNormal]; //输入背景框
        //改变提示文字颜色
        UITextField *sbTextField = [searchBar valueForKey:@"searchField"]; //首先取出textfield
        sbTextField.textColor = [UIColor blueColor]; //输入的颜色
        UILabel *sbPlaceholderLabel = [sbTextField valueForKey:@"placeholderLabel"]; //然后取出textField的placeHolder
        sbPlaceholderLabel.textColor = [UIColor redColor]; //改变颜色
        searchBar.delegate = self;
        //取消按钮有显示的时候
        searchBar.showsCancelButton = YES;
        UIButton *cancelBtn = [searchBar valueForKey:@"cancelButton"]; //首先取出cancelBtn
        cancelBtn.enabled = YES;
        //这样就可以随便设置这个按钮了
        [cancelBtn setTitle:@"搜索" forState:UIControlStateNormal];
        [self.view addSubview:searchBar];
    

    相关文章

      网友评论

        本文标题:SearchBar自定义

        本文链接:https://www.haomeiwen.com/subject/spykfxtx.html