Because of the need for interface beautification, the original SearchBar control is too ugly and lazy to study the new style of the SearchBar control, so I decided to rewrite a search function control.
Creates a UITextField class object.
[Cpp] searchField = [[UITextField alloc] initWithFrame: CGRectMake (,)] autorelease];
SearchField. textColor = [UIColorcolor WithRed: 0.0/255.0 green: 103.0/255.0 blue: 155.0/255.0 alpha: 1.0];
SearchField. font = [UIFont systemFontOfSize: 16];
SearchField. backgroundColor = [UIColor blueColor];
SearchField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;
[SearchField addTarget: self action: @ selector (textFieldDidChange :) forControlEvents: UIControlEventEditingChanged]; // event when textField text changes
SearchField = [[[UITextField alloc] initWithFrame: CGRectMake (,)] autorelease];
SearchField. textColor = [UIColorcolor WithRed: 0.0/255.0 green: 103.0/255.0 blue: 155.0/255.0 alpha: 1.0];
SearchField. font = [UIFont systemFontOfSize: 16];
SearchField. backgroundColor = [UIColor blueColor];
SearchField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter;
[SearchField addTarget: self action: @ selector (textFieldDidChange :) forControlEvents: UIControlEventEditingChanged]; // event when textField text changes
Set ReturnKeyType to UIRetuirKeySearch:
[Cpp] [searchField setReturnKeyType: UIReturnKeySearch];
[SearchField setReturnKeyType: UIReturnKeySearch];
Set the delegate of UITextField to self:
[Cpp] searchField. delegate = self;
SearchField. delegate = self;
Then write the function that responds to the event by clicking the search button:
[Cpp]-(BOOL) textFieldShouldReturn :( UITextField *) theTextField {
[TheTextField resignFirstResponder];
NSLog (@ "do something what you want ");
ReturnYES;
}
-(BOOL) textFieldShouldReturn :( UITextField *) theTextField {
[TheTextField resignFirstResponder];
NSLog (@ "do something what you want ");
ReturnYES;
} When the text content of textField changes, the event processing function
[Cpp]-(void) textFieldDidChange :( UITextField *) TextField {
NSLog (@ "textFieldDidChange ");
If (! [TextField. textisEqualToString: @ ""]) {
DelButton. hidden = NO; // copy the subfork behind the searchbar
} Else {
DelButton. hidden = YES;
}
}
-(Void) textFieldDidChange :( UITextField *) TextField {
NSLog (@ "textFieldDidChange ");
If (! [TextField. textisEqualToString: @ ""]) {
DelButton. hidden = NO; // copy the subfork behind the searchbar
} Else {
DelButton. hidden = YES;
}
}
Ha, of course I still refer to the following a high person idea: http://forums.macrumors.com/archive/index.php/t-523664.html
Note: This article was previously published on baidu, but it cannot match baidu's typographical style.
From the column zcl316369