菜鸟教程小白 发表于 2022-12-13 11:35:38

ios - 如何在 iOS 中为 UISearchBar 设置最近的搜索历史下拉列表


                                            <p><p>我想为 <strong><code>UISearchbar</code></strong> 设置最近的搜索历史下拉列表。我已经使用 <code>UITableView</code> </p> 以编程方式实现了一个下拉菜单

<p><strong>逻辑:</strong><br/>
我的实现方式是在 <code>UISearchBar</code> 正下方设置一个 <code>UITableView</code> 并单击 <code>UISearchBar</code> 它将弹出。
我最初在 <code>viewdidload</code> 和 <code> 中隐藏了该 TableView
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar</code> 正在取消隐藏 tableview 。
我面临着太多的问题:</p>

<p><strong>1.</strong> 当 <code>searchbartext</code> 为空时,返回按钮未启用。在这种情况下,如果我必须再次取消/隐藏表格 View ,我必须依赖<code>点击手势</code> 。 </p>

<p><strong>2.</strong> 再次点击 <code>searchbartext</code> 不会触发它的任何代理。它触发代表点击 <code>searchbartext</code> 的唯一时间是第一次点击它然后它调用 <code>-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar</code> </p>

<p>你们还有其他解决方案/方法可以比这更好的方式实现此功能吗?
我们是否有任何定制的 <code>UISearchBar</code> 最近历史下拉列表??<br/>
我已经在 <code>cocoacontrols</code>、<code>code4app</code> 和 <code>cocoapods</code> 中搜索了一个自定义的 <code>UISearchBar</code> 最近历史下拉列表,因为没有希望找到任何 。
请帮我解决这个问题。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>对于您的<strong>第一个问题</strong>,您可以在 <code>UISearchBar</code> 上添加一个类似这样的类别,这将始终启用键盘上的 <code>return</code> 按钮点击 <code>UISearchBar</code></p>

<pre><code>@implementation UISearchBar (MyAddition)


- (void)alwaysEnableSearch {
    // Loop around subviews of UISearchBar
    NSMutableSet *viewsToCheck = ];

    while ( &gt; 0) {
      UIView *searchBarSubview = ;
      ;
      ;

    if () {
      @try {
            // Force return key to be enabled
            [(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
      }
      @catch (NSException *iException) {

      }
    }
}
}
</code></pre>

<p>对于<strong>第二个问题</strong>,为什么不使用 <code>shouldChangeTextInRange:</code> 委托(delegate)方法,该方法会为每个输入的字符调用。</p>

<p>作为旁注,<a href="https://stackoverflow.com/questions/29814688/add-a-drop-down-suggestion-below-a-uisearchbar-and-uitextfields" rel="noreferrer noopener nofollow">this SO thread</a>有一个示例代码来执行此操作。这可能会对您有所帮助。</p>

<p>祝你好运!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 中为 UISearchBar 设置最近的搜索历史下拉列表,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33316915/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33316915/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 中为 UISearchBar 设置最近的搜索历史下拉列表