gpt4 book ai didi

UISearchBar 取消按钮

转载 作者:行者123 更新时间:2023-12-04 01:28:25 30 4
gpt4 key购买 nike

在 iOS 5 上使用带有 showCancelButton=YES 的 UISearchBar。希望在键盘下拉时取消按钮保持启用状态。使用以下代码似乎不起作用:

for (id subView in self.searchControl.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton *)subView;
[cancelButton setEnabled:YES];
break;
}
}
subView 实际上是一个 UINavigationButton,它似乎不是 UIButton 的子类。我在这里想念什么??????在 Apple 文档中也找不到有关 UINavigationButton 类的任何信息。

最佳答案

设置您的搜索栏委托(delegate),然后放置此代码。

- (void) searchBarSearchButtonClicked:(UISearchBar*) theSearchBar
{
[theSearchBar resignFirstResponder];
[theSearchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}

swift 3.0
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
searchBar.setShowsCancelButton(false, animated: true)
}

关于UISearchBar 取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8536625/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com