Enable cancel button with UISearchBar in iOS8

只谈情不闲聊 提交于 2019-12-04 09:59:11

Here's a working solution for iOS 8 and 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 

did the trick for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!