UINavigationController 中的 UISearchBar 无法选择取消按钮

发布于 2024-12-08 16:49:13 字数 697 浏览 0 评论 0原文

我已经将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2024-12-15 16:49:13

我肯定会推荐一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文