作者:寻找冄己靉 | 来源:互联网 | 2023-07-31 20:08
在修改searchBar上面的placeholder字体颜色时,我自己手写的代码跟正确的一模一样时,它识别不出来,总是崩,错误内容说是没有那个value,真是见鬼了。当我粘贴过来时,它
在修改searchBar上面的placeholder字体颜色时,我自己手写的代码跟正确的一模一样时,它识别不出来,总是崩,错误内容说是没有那个value,真是见鬼了。当我粘贴过来时,它就好了。爱,真是那个什么了………………
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(20 , 20, kUISCREEN_WIDTH - 40, 30)];
_searchBar.placeholder = @"个合格";
_searchBar.tintColor = [UIColor whiteColor];
_searchBar.translucent = YES;
_searchBar.layer.masksToBounds = YES;
_searchBar.layer.cornerRadius = 5.0;
_searchBar.alpha = 0.2;
[_searchBar setBackgroundImage:[UIImage new]];
[_searchBar setTranslucent:YES];
_searchBar.searchBarStyle = UISearchBarStyleProminent;
searchBar.barTintColor = [UIColor whiteColor];
[_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"backgroundImage"] forState:UIControlStateNormal];
UITextField * searchField = [_searchBar valueForKey:@"_searchField"];
[searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
[topBackImageView addSubview:self.searchBar];
//之前的效果
//之后的效果
其他:
#import "ViewController.h"
@interface ViewController ()<UISearchBarDelegate>
@property(nonatomic, strong)UISearchBar * searchBar;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 80, 300, 50)];
_searchBar.barStyle = UIBarStyleDefault;
_searchBar.searchBarStyle = UISearchBarStyleDefault;
_searchBar.text = @"搜索框";
_searchBar.placeholder = @"占位符";
_searchBar.tintColor = [UIColor yellowColor];
_searchBar.barTintColor = [UIColor redColor];
_searchBar.translucent = YES;
_searchBar.showsCancelButton = YES;
_searchBar.showsBookmarkButton = YES;
_searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(50, 0);
_searchBar.searchTextPositionAdjustment = UIOffsetMake(50, 0);
[_searchBar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[_searchBar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
_searchBar.delegate = self;
[self.view addSubview:self.searchBar];
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
NSLog(@"ShouldBegin");
return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
NSLog(@"DidBegin");
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
NSLog(@"ShouldEnd");
return YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
NSLog(@"DidEnd");
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"DidChange");
if(searchText != nil && searchText.length > 0){
[self.searchDataAry removeAllObjects];
for (SearchModel * model in self.originAry) {
if ([model.shop_name rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
[self.searchDataAry addObject:model];
}
}
[self.tableView reloadData];
}else{
self.searchDataAry = [NSMutableArray arrayWithArray:self.originAry];
[self.tableView reloadData];
}
}
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"shouldChange");
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(@"SearchButtonClicked");
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
NSLog(@"BookmarkButtonClicked");
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"CancelButton");
}
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{
NSLog(@"ResultsListButton");
}