6

我想改变的外观UISearchBar:所以,

如果有一种方法可以让我的 UITextField(在搜索的自定义背景中)发挥作用UISearchBar?或子类化和覆盖- (void)layoutSubviews是唯一的方法?

请告诉如何子类化它!

4

6 回答 6

6

UISearchBar您可以使用以下代码更改背景..

for (UIView *subview in searchBar.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
            bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SkyBackground.jpeg"]];
            [searchBar insertSubview:bg aboveSubview:subview];
            [subview removeFromSuperview];
            break;
        }
    }

从此代码中,您可以为背景设置图像或其他任何内容UISearchBar

另请参阅我的这个答案以及其他一些更改搜索栏组件布局的功能。如何在 iOS 上更改 UISearchBar 组件的内部背景颜色

更新:

要更改取消按钮的外观,请使用以下代码...

   UIButton *cancelButton = nil;

    for(UIView *subView in searchBar.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
            //do something here with this cancel Button
        }
    }
于 2013-01-03T07:07:18.557 回答
6

它也可以使用 UITextFieldDelegates

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    
NSString *searchString;
if (string.length > 0) {        
    searchString = [NSString stringWithFormat:@"%@%@",textField.text, string];
} else {
   searchString = [textField.text substringToIndex:[textField.text length] - 1];
}
NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", searchString];
resultArray = [[resultTempArray filteredArrayUsingPredicate:filter] mutableCopy];

if (!searchString || searchString.length == 0) {

    resultArray = [resultTempArray mutableCopy];
} else {
    if (resultArray.count == 0) {
NSLog(@"No data From Search");
    }
}    
[tableView reloadData];    
return YES;
}
于 2013-05-24T08:17:20.667 回答
2

使用 UItextfield 委托方法进行过滤

#pragma mark - UITEXTFIELD DELEGATE METHODS -

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    arrFilterSearch = nil;
    arrFilterSearch = [NSMutableArray arrayWithArray:arrSearch];
    UITableView *tblView = (UITableView *)[self.view viewWithTag:tblView_Tag];
    [tblView reloadData];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *searchKey = [textField.text stringByAppendingString:string];
    arrFilterSearch = nil;
    searchKey = [searchKey stringByReplacingCharactersInRange:range withString:@""];
    if (searchKey.length) {
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"firstname contains[cd] %@", searchKey];
            arrFilterSearch = [NSMutableArray arrayWithArray:[arrSearch filteredArrayUsingPredicate:pred]];
    }
    else
        arrFilterSearch = [NSMutableArray arrayWithArray:arrSearch];

    UITableView *tblView = (UITableView *)[self.view viewWithTag:tblView_Tag];
    [tblView reloadData];
    return YES;
}
于 2014-05-23T07:59:28.283 回答
1

尝试这个

    self.TextFieldSearch = (UITextField *)[self.Search.subviews lastObject];
    [self.TextFieldSearch removeFromSuperview];
    [self.view addSubview:self.TextFieldSearch];
    self.TextFieldSearch.delegate = self;
    self.TextFieldSearch.font = [UIFont systemFontOfSize:10];
    self.TextFieldSearch.keyboardAppearance =  UIKeyboardAppearanceAlert;
    [self.Search removeFromSuperview];
于 2013-01-03T07:08:46.927 回答
0

更改 UIsearchBar 背景是获得自定义 UISearchBar 的一种方法。但是,如果您想要有限的搜索表功能,您可以使用普通文本字段并连接搜索按钮以过滤另一个数组中的 tableview 的结果并使用该数组更新 tabledata。

UIsearchBar 的- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 几乎等同于UItextField's valieChanged IBAction

于 2013-01-03T08:23:53.660 回答
0

好的,我遇到了同样的问题,我将 UISearchBar 子类化了!干得好

- (void) layoutSubviews {

[super layoutSubviews];
UITextField *searchField = nil;
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
        }

        if ([subview isKindOfClass:[UITextField class]]) {
            searchField = (UITextField *)subview;
            searchField.leftViewMode = UITextFieldViewModeNever;
            [searchField setValue:[UIFont systemFontOfSize:8] forKeyPath:@"_placeholderLabel.font"];
             [searchField setBackground: [UIImage imageNamed:@"images.png"] ];
              searchField.clearButtonMode = UITextFieldViewModeNever;
        }

}
self.showsCancelButton = NO;
}

输入文字时

于 2013-07-10T05:47:12.510 回答