在iOS8中使用UISearchBar启用取消按钮

有没有任何方法来启用UISearchBar的“取消”按钮? 现在每当我呼叫resignFirst响应者时,取消按钮都会被禁用。 只有当我再次点击搜索栏时,取消才会启用。 无论如何停止禁用取消按钮?


这是iOS 8和Swift的工作解决方案。

func enableCancleButton (searchBar : UISearchBar) {
    for view1 in searchBar.subviews {
        for view2 in view1.subviews {
            if view2.isKindOfClass(UIButton) {
                var button = view2 as! UIButton
                button.enabled = true
                button.userInteractionEnabled = true
            }
        }
    }
}

对于iOS7:

- (void)enableCancelButton{
    for (UIView *view in self.subviews){
        for (id subview in view.subviews){
            if ([subview isKindOfClass:[UIButton class]]){
                [subview setEnabled:YES];
                return;
            }
        }
    }
}

要使上述代码在iOS8中可用,需要在启用子视图之前添加延迟:

- (void)enableCancelButton{
    for (UIView *view in self.subviews){
        for (id subview in view.subviews){
            if ([subview isKindOfClass:[UIButton class]]){
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10), dispatch_get_main_queue(), ^{
                    [subview setEnabled:YES];
                });
                return;
            }
        }
    }
}

UIBarButtonItem.appearance().enabled = true 

为我做了诡计

链接地址: http://www.djcxy.com/p/44245.html

上一篇: Enable cancel button with UISearchBar in iOS8

下一篇: How to trigger cancel button in UISearchBar?