[英]how to hide keyboard when i touch screen in iOS
i am creating search bar
in iOS
when i press search bar keyboard will be appeared but after the search complete i want keyboard will be disappeared on screen. 我在
iOS
创建search bar
时,我按下搜索栏键盘会出现但搜索完成后我想键盘将在屏幕上消失。 i am implement code but its not hide . 我是实现代码,但它不隐藏。
//this methos show to create dynamic search bar
-(void)SearchBarCode
{
//[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
self.disableViewOverlay = [[UIView
alloc]initWithFrame:CGRectMake(0.0f,44.0f,320.0f,416.0f)];
self.disableViewOverlay.backgroundColor=[UIColor blackColor];
self.disableViewOverlay.alpha = 0;
theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(-61, 46, 378.0f, 50)];
theSearchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 46, 310.0,
44.0)];
searchBarView.autoresizingMask = 0;
theSearchBar.delegate =self;
[searchBarView addSubview:theSearchBar];
self.navigationItem.titleView=searchBarView;
scrollView.backgroundColor = [UIColor brownColor];
theSearchBar.placeholder = @"Search";
theSearchBar.delegate = self;
}
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar becomeFirstResponder];
}
- (void)viewDidAppear:(BOOL)animated
{
[self.theSearchBar resignFirstResponder];
[super viewDidAppear:animated];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[theSearchBar setShowsCancelButton:NO animated:NO];
[theSearchBar becomeFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
theSearchBar.text = @"";
[theSearchBar setShowsCancelButton:YES animated:YES];
[theSearchBar resignFirstResponder];
}
- (void)searchTableList
{
NSString *searchString = theSearchBar.text;
filteredContentList = [[NSMutableArray alloc]init];
for (SMSCategory *cat in Arrayobject)
{
NSString *tempStr = cat.Name;
NSComparisonResult result = [tempStr compare:searchString options:
(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,
[searchString length])];
if (result == NSOrderedSame)
{
[filteredContentList addObject:cat];
}
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
}
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active
{
self.theTableView.allowsSelection = !active;
self.theTableView.scrollEnabled = !active;
if (!active)
{
[disableViewOverlay removeFromSuperview];
[theSearchBar resignFirstResponder];
}
else
{
self.disableViewOverlay.alpha = 0;
[self.view addSubview:self.disableViewOverlay];
[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
self.disableViewOverlay.alpha = 0.6;
[UIView commitAnimations];
// probably not needed if you have a details view since you
// will go there on selection
NSIndexPath *selected = [self.theTableView
indexPathForSelectedRow];
if (selected)
{
[self.theTableView deselectRowAtIndexPath:selected
animated:NO];
}
}
[theSearchBar setShowsCancelButton:active animated:YES];
}
.h File code .h文件代码
@interface CategoryMainWindowViewController : UIViewController<UISearchBarDelegate,UITextViewDelegate>{
NSMutableArray *Arrayobject;
UIView *disableViewOverlay;
UISearchBar *theSearchBar;
NSMutableArray *filteredContentList;
BOOL isSearching;
}
@property(retain) UIView *disableViewOverlay;
@property(retain) NSMutableArray *Arrayobject;
@property (nonatomic, retain) IBOutlet UITableView *theTableView;
@property (nonatomic, retain) IBOutlet UISearchBar *theSearchBar;
@property(nonatomic,retain) UIScrollView *scrollView;
@property(strong,nonatomic) NSString *databasePath;
- (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active;
- (void)DynamicButton:(NSMutableArray*)objectName;
- (NSMutableArray*)GetData;
@end
add this method to your view 将此方法添加到您的视图中
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touching on screen");
[self.view endEditing:YES];
}
After searching
happens you should call your dissmissKeyBoard
method. searching
后你应该调用你的dissmissKeyBoard
方法。
- (void) dismissKeyboard
{
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
This can not work as "becomeFirstResponder" shows the keyboard instead of hiding it! 这不能工作,因为“becomeFirstResponder”显示键盘而不是隐藏它! The hide-command would be resignFirstResponder, like this:
hide-command将是resignFirstResponder,如下所示:
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
Edit: If the only thing you want is to hide the keyboard when the user has searched, it would be easier to just implement the UISearchBarDelegate -method 编辑:如果你想要的只是在用户搜索时隐藏键盘,那么实现UISearchBarDelegate -method会更容易
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
and call [self dismissKeyboard] from there, like this: 并从那里调用[self dismissKeyboard],如下所示:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
[self dismissKeyboard];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.