iOS Change the title of cancel button in UISearchBar

前端 未结 13 1893
隐瞒了意图╮
隐瞒了意图╮ 2020-12-28 10:35

I wish to change the title of the cancel button in iOS. I have been using this previously:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayCon         


        
13条回答
  •  伪装坚强ぢ
    2020-12-28 11:02

    here is my solution for both ios6 & ios7

    #define IS_IOS7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    {
        self.searchDisplayController.searchBar.showsCancelButton = YES;
    
        UISearchBar *searchBar = self.searchDisplayController.searchBar;
        UIView *viewTop = IS_IOS7 ? searchBar.subviews[0] : searchBar;
        NSString *classString = IS_IOS7 ? @"UINavigationButton" : @"UIButton";
    
        for (UIView *subView in viewTop.subviews) {
            if ([subView isKindOfClass:NSClassFromString(classString)]) {
                UIButton *cancelButton = (UIButton*)subView;
                [cancelButton setTitle:@"your title" forState:UIControlStateNormal];
            }
        }
    }
    

提交回复
热议问题