UINavigationController 中的 UISearchBar 无法选择取消按钮
我已经将 UISearchBar 添加到 UINavigationController 中,但是一旦将取消按钮添加到搜索栏,我就无法再通过触摸来选择它。
我用来创建搜索栏并添加它的代码是:
UISearchBar *theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,0.0f)];
theSearchBar.delegate = self;
[theSearchBar setPlaceholder:@"Search iPad"];
[theSearchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.titleView = theSearchBar;
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[theSearchBar release];
如果我删除添加取消按钮的行,我就可以专注于搜索栏并且搜索可以正常工作。一旦我输入该行,它就会正确显示,但我无法再关注它。
我尝试将其放入一个简单的 UIView 容器中,但这也没有解决问题。
添加取消按钮会导致失败吗?
更新: 我最终没有使用取消按钮,因为我确定它没有必要像搜索栏的使用方式那样。
I have added a UISearchBar to the UINavigationController just fine, but as soon as I add the cancel button to the search bar I can no longer select it by touching it.
The code I'm using to create the search bar and add it is:
UISearchBar *theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,0.0f)];
theSearchBar.delegate = self;
[theSearchBar setPlaceholder:@"Search iPad"];
[theSearchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.titleView = theSearchBar;
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[theSearchBar release];
If I remove the line to add the cancel button I'm able to focus on the search bar and the search works correctly. As soon as I put that line in, it displays properly, but I can no longer focus on it.
I've tried putting it into a simple UIView container, but that didn't solve the problem either.
What about adding the cancel button could be causing this to fail?
UPDATE:
I ended up not using the cancel button because I determined it wasn't necessary the way the search bar was being used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我肯定会推荐一个 UISearchDisplayController:苹果文档。
它内置了一个取消按钮,通过将其放置在界面生成器中,您可以节省大量代码。更少的代码=更少的错误。
I would definitely recommend a UISearchDisplayController: apple docs.
It has a cancel button built right in, and by placing it in interface builder you can save a lot of code. Less code = less bugs.