I have created one custom Searchbar with Cancle button. I want to Enable cancel button Everytime.but it enable only after i start editing search bar. I wrote following Code.
#pragma mark - Search Bar Methods
-(void)addSearchBar{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 65, 320, 44)];
[self.view addSubview:self.searchBar];
self.searchBar.showsCancelButton = YES;
self.searchBar.delegate = self;
for (UIView *searchBarSubview in [self.searchBar subviews]) {
if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
@try {
// set style of keyboard
[(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];
// always force return key to be enabled
[(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
}
@catch (NSException * e) {
// ignore exception
}
}
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchBar resignFirstResponder];
[self.searchBar removeFromSuperview];
[btnSearch setEnabled:TRUE];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
}
But i am not able to get textField from its subviews. Please Help.