转载的
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
0人点赞
作者:花田半亩1992
链接:https://www.jianshu.com/p/f6054c965480
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论