我有同样的问题。按钮在那里,但 titleColor 与背景相同。我所做的修复它是搜索搜索栏的取消按钮子视图并将按钮的标题颜色设置为不同的颜色。请注意在显示取消按钮后设置此项,如果在显示取消按钮之前,视图不存在。我的代码:
[self.mySearchBar setShowsCancelButton:YES animated:YES];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
for(id subview in [self.mySearchBar subviews])
{
if ([subview isKindOfClass:[UIButton class]]) {
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
}
} else {
for(id subview in [[[self.mySearchBar subviews] objectAtIndex:0] subviews])
{
if ([subview isKindOfClass:[UIButton class]]) {
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
}
}
另请注意,在 iOS 7 中,取消按钮隐藏在搜索栏的第一个子视图中。在 iOS 7 之前,它是搜索栏的直接子视图。