Enable cancel button with UISearchBar in iOS8

Is there any method to enable 'Cancel' button of UISearchBar? Now whenever I call resignFirst responder, cancel button gets disabled. Only when I tap over the search bar again, cancel gets enabled. Is there anyway to stop disabling the cancel button?


这是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
            }
        }
    }
}

For iOS7:

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

To make the above code work in iOS8, you need add a delay before enable the subview:

- (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/44246.html

上一篇: 如何在仅使用UISearchBar时触发“取消搜索”?

下一篇: 在iOS8中使用UISearchBar启用取消按钮