make UItextfield to function like UISearchBar

放肆的年华 提交于 2019-12-21 13:01:49

问题


I want to change the looks of UISearchBar: So,

If there's a way by which I can make my UITextField(in a custom background for search) to function like UISearchBar ? or subclassing and overriding the - (void)layoutSubviews is the only way ?

Kindly tell How to subclass it !!!


回答1:


you can change the UISearchBar background using this bellow code..

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;
        }
    }

From this code you can set image or anything else for background of UISearchBar.

Also see my this answer with some other functionality to change the searchbar component layout..How to change inside background color of UISearchBar component on iOS

UPDATE:

for change the Cancel button appearance use this bellow code...

   UIButton *cancelButton = nil;

    for(UIView *subView in searchBar.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
            //do something here with this cancel Button
        }
    }



回答2:


Its also possible using 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;
}



回答3:


Filtering using UItextfield delegate methods

#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;
}



回答4:


Try this

    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];



回答5:


Changing the UIsearchBar background is one way to get a customized UISearchBar. But if you want limited functionality of searching a table you can have normal text Field and hook up a search button to filter results of tableview in another array and update tabledata with that array.

UIsearchBar's - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText is almost equivalent to UItextField's valieChanged IBAction




回答6:


Ok, I got the same issue, and I subclassed UISearchBar! Here you go

- (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;
}



来源:https://stackoverflow.com/questions/14134443/make-uitextfield-to-function-like-uisearchbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!