UISearchBar - 触摸时键盘不显示
Posted
技术标签:
【中文标题】UISearchBar - 触摸时键盘不显示【英文标题】:UISearchBar - Keyboard NOT displaying when touched 【发布时间】:2011-07-16 16:22:19 【问题描述】:当我在 iphone 应用程序中触摸 UISearchBar 时,没有显示键盘。我必须在 touchesBegan 中使用 [searchBar becomeFirstResponder] 之类的东西吗?
我的 searchBarShouldBeginEditing 返回 YES。
我已经尝试使用 [seachBar becomeFirstResponder] 并且我的代表按我的预期被调用。我看到有一个 UISearchDisplayController 但我认为 UISearchBar 可能很容易实现。
我的自定义控制器类是 UIViewController 的子类。
UIImageView *topImage = [[UIImageView alloc] initWithImage:
[UIImage imageWithContentsOfFile:
[myBundle pathForResource:@"top_header"
ofType:@"png"inDirectory:@"Images/facebook_friend"]]];
CGRect viewRect = CGRectMake(0, 0, 320, 480);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 310)
style:UITableViewStylePlain];
self.tableView.bounces = YES;
UIImageView *bottomImage = [[UIImageView alloc] initWithImage:
[UIImage imageWithContentsOfFile:
[myBundle pathForResource:@"bottom_background" ofType:@"png"
inDirectory:@"Images/facebook_friend"]]];
bottomImage.center = CGPointMake(bottomImage.center.x, 480-bottomImage.frame.size.height);
// so that we can push a button inside it
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,0)];
[searchBar setPlaceholder:@"Search"];
[searchBar setDelegate:self];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
tableView.delegate = self;
tableView.dataSource = dataSource;
[tableView reloadData];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setFrame:CGRectMake(0, 0, 76, 43)];
backButton.center = CGPointMake(210, 40);
[backButton setBackgroundImage:[UIImage imageNamed:@"Images/facebook_friend/back-button.png"]
forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backClick:)
forControlEvents:UIControlEventTouchUpInside];
// spinner to show we're loading
self.spinner = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(125,100,50,50)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[tableView addSubview:spinner];
// top search section
[myView addSubview:topImage];
[topImage addSubview:searchBar];
// friend list
[myView addSubview:tableView];
// bottom button section
[bottomImage addSubview:backButton];
[bottomImage addSubview:postButton];
[myView addSubview:bottomImage];
[topImage setUserInteractionEnabled:YES];
[bottomImage setUserInteractionEnabled:YES];
[myView setUserInteractionEnabled:YES];
self.view = myView;
编辑:周日上午 9:50
如果我把所有的子视图都拿出来,只用 searchBar 添加 myView,我只会看到 myView 的触摸事件。但是,如果我只是设置
self.view=searchBar
然后触摸搜索栏会调出键盘。这是一个非常小的例子:
CGRect viewRect = CGRectMake(0, 0, 320, 480);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,0)];
[searchBar setPlaceholder:@"Search"];
[myView addSubview:searchBar];
self.view = myView;
【问题讨论】:
这是一个非常奇怪的错误,就好像您只是放置搜索栏而不定义委托或任何方法,然后在点击搜索栏文本字段时显示键盘。你确定你没有做一些不寻常的事情吗?你能发布一些猜测吗? XIB 截图? 【参考方案1】:我的 UISearchBar 的高度为 0,这就是我点击时它没有显示键盘的原因。
【讨论】:
以上是关于UISearchBar - 触摸时键盘不显示的主要内容,如果未能解决你的问题,请参考以下文章