简体   繁体   English

UISearchBar - 当点击取消按钮时,如何防止关闭 scopeBar?

[英]UISearchBar - How can I prevent dismissing the scopeBar when the cancel button is tapped?

I want my UISearchBar 's scopeButtons to remain visible at all times, but even though I've set searchBar.showsScopeBar = YES' , the buttons are still dismissed with an animation if I begin a search and then cancel it.我希望我的UISearchBar的 scopeButtons 始终保持可见,但即使我设置searchBar.showsScopeBar = YES' ,如果我开始搜索然后取消它,按钮仍然会被 animation 关闭。

Is there any way I can prevent the scopeBar being animated out when the search is cancelled?当搜索被取消时,有什么办法可以防止 scopeBar 被激活?

Since the scope bar is only meant to be used while the UISearchController is active, I approached the problem differently.由于 scope 栏仅在UISearchController处于活动状态时使用,因此我以不同的方式解决了这个问题。

I make the UISearchController active immediately.我立即使UISearchController处于活动状态。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // Immediately activating the searchController keeps scope bar permanently visible
    searchController.isActive = true
    // Tapping the cancel button triggers this method, which quickly toggles the scope bar
    // But the cancel button isn't needed anyway, so hiding it solves the problem
    searchController.searchBar.showsCancelButton = false
}

But now that the UISearchController is always active, by default the UINavigationBar is hidden, which is probably not what you want.但是现在UISearchController始终处于活动状态,默认情况下UINavigationBar是隐藏的,这可能不是您想要的。 To fix that, I prevent it from being hidden in viewDidLoad为了解决这个问题,我防止它被隐藏在viewDidLoad

searchController.hidesNavigationBarDuringPresentation = false

This approach means that I don't need to use showsScopeBar at all, because it is always visible while the UISearchController is active.这种方法意味着我根本不需要使用showsScopeBar ,因为它在UISearchController处于活动状态时始终可见。

In case anyone is interested, the following delegate method does it.如果有人感兴趣,下面的委托方法可以做到。

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsScopeBar:YES];
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM