UISearchController的cancel按钮自定义中文取消

iOS13 UISearchBar的层级发生了变化


iOS13 中取消了私有KVC

其中UISearchBar

[searchBar setValue:@"xxx "forKey:@"_cancelButtonText"];

一旦命中就crash

将cancel设为中文“取消”方法如下

    self.searchController.searchBar.showsCancelButton = YES;

    NSArray*searchBarSubViews = [self.searchController.searchBarsubviews];//UIView

    UIView*view = [searchBarSubViewsobjectAtIndex:0];

    NSArray*searchBarContainerView = [[[viewsubviews]objectAtIndex:1] subviews];//UISearchBarContainerView

    for(UIView*viewinsearchBarContainerView) {

        if ([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {

                    UIButton*cancelButton = (UIButton*)view;

                    [cancelButtonsetTitle:@"取消"forState:UIControlStateNormal];

                }

    }

注意:一定要提前把showsCancelButton 设置为YES,否则UISearchBarContainerView第一次的subviews没有UINavigationButton

你可能感兴趣的:(UISearchController的cancel按钮自定义中文取消)