作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我按搜索栏时会在search bar
中创建iOS
,但会出现键盘,但搜索完成后,我希望键盘会在屏幕上消失。
我正在执行代码,但不是隐藏的。
//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];
}
@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
最佳答案
这不能工作,因为“becomeFirstResponder”显示键盘而不是隐藏键盘! hide-命令将是resignFirstResponder,如下所示:
- (void) dismissKeyboard
{
// add self
[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSLog(@"Search Clicked");
[self searchTableList];
[self DynamicButton:filteredContentList];
[self dismissKeyboard];
}
关于ios - 如何在iOS中触摸屏时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355666/
我是一名优秀的程序员,十分优秀!