UISearchBar取消按钮

时间:2011-12-16 15:50:32

标签: uisearchbar

在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类的任何信息。

6 个答案:

答案 0 :(得分:21)

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

- (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)
}

答案 1 :(得分:2)

只是为了改善Kurt Spindler在他的帖子上说的话。虽然这可能并不优越,但它更受欢迎。我使用dispatch来做同样的事情。

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    for (UIView *subview in searchBar.subviews)
    {
        if ([subview isKindOfClass:[UIButton class]])
        {
            int64_t delayInSeconds = .001;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                UIButton * cancelButton = (UIButton *)subview;
                [cancelButton setEnabled:YES];
            });
            break;
        }
    }
}

这应该适用于需要帮助的所有人保持取消。确保稍后在单击取消或搜索时隐藏它。

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

答案 2 :(得分:1)

我必须在我的应用程序中解决同样的问题。在分数延迟后尝试执行上述代码,例如

[self performSelector:@selector(delayedEnable:) withObject:cancelButton afterDelay:0.001];

- (void)delayedEnable:(UIButton*)button {
    button.enabled = YES;
}

这很难看,但这就是为我工作所需要的。或者,如果您实际使用UISearchDisplayController来显示结果,它还应该为您修复取消按钮行为(我认为 - 我已经深入研究了这个问题)。

答案 3 :(得分:1)

我在搜索栏取消按钮上放置了一个自定义取消按钮。

答案 4 :(得分:1)

我知道这个已经老了,但是我能够解决它而且我找不到任何其他东西来解决它,所以这里有效(在Swift 3中):

键盘隐藏时启用取消按钮。将其添加到viewDidLoad():

body {
  font: 10px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis path {
  display: none;
}

.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.overlay {
  fill: none;
  pointer-events: all;
}

.focus circle {
  fill: none;
  stroke: steelblue;
}

在keyboardNotification(notification :)方法中,对keyboardDidHide通知作出反应:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)

enableSearchCancelButton取自其他人已回复的内容here

func keyboardNotification(notification: NSNotification) {
    if notification.name == Notification.Name.UIKeyboardDidHide {
        self.enableSearchCancelButton()
    }
}

最后,不要忘记以观察者的身份删除视图控制器:

func enableSearchCancelButton() {
    //enable the cancel button
    for view1 in searchBar.subviews {
        for view2 in view1.subviews {
            if let button = view2 as? UIButton {
                button.isEnabled = true
                button.isUserInteractionEnabled = true
            }
        }
    }
}

答案 5 :(得分:0)

快捷键4:

if let btnCancel = searchBar.value(forKey: "cancelButton") as? UIButton {
    btnCancel.isEnabled = true
}