UIToolbar 中的 UISearchBar 不会显示取消按钮

发布于 2024-10-06 15:26:11 字数 221 浏览 0 评论 0原文

我有一个 UISearchBar 作为 UIToolbar 中的 UIBarButtonItem 嵌套。基本功能有效,但取消按钮和范围栏拒绝显示。我尝试在界面生成器中启用它们,并且还尝试手动调用 [searchBar setShowsCancelButton:YES],但这两种方法都不起作用。有什么想法吗?

这是在 iPad 上。我在 iOS 3 中尝试过,在 4.2 中也不起作用。

I have a UISearchBar nested as a UIBarButtonItem within a UIToolbar. The basic functionality works, but the cancel button and the scope bar refuse to display. I've tried enabling them in the interface builder, and I've also tried manually calling [searchBar setShowsCancelButton:YES], but neither method works. Any ideas?

This is on an iPad. I tried in iOS 3, and it doesn't work in 4.2, either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萌辣 2024-10-13 15:26:11

您需要用另一个视图包装 UISearchBar。

UISearchBar *searchBar = [UISearchBar new];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchBar];
UIBarButtonItem *searchBarItem =
    [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];

You need to wrap the UISearchBar with another view.

UISearchBar *searchBar = [UISearchBar new];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchBar];
UIBarButtonItem *searchBarItem =
    [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
孤蝉 2024-10-13 15:26:11

您只是遇到了 UISearchBar 工作方式的限制。一种解决方法是将 UISearchBar 分层放置在 UIToolbar 上,而不是让它真正位于工具栏中。这在视觉上是有效的,因为搜索栏的绘制就像工具栏一样 - 它们的渐变是相同的。

You've simply hit a limitation on how UISearchBar works. One workaround is to layer the UISearchBar over the UIToolbar rather than having it genuinely live in the toolbar. This works visually because a search bar is painted just like a toolbar - their gradients are the same.

眼角的笑意。 2024-10-13 15:26:11

真的有必要在搜索栏中使用取消按钮吗?只需使用 textDidChange 方法来查看字符串是否已被清除,并使用它来执行与取消相同的操作。

is it really necessary to ever use a Cancel button in a search bar? Just use the textDidChange method to see if the string has been cleared and use that to do the same thing the cancel would.

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