两种方法
1:直接遍历searchBar子视图,这种方法不太好,因为随着版本升级它内部层级可能会发生变化(例如IOS11的导航栏,懂得自然懂)
2:取私有变量
UIButton *cancel = [self valueForKey:@"_cancelButton"];
[cancel setTitle:@"取消" forState:UIControlStateNormal];
//这种状态不写是不行的
[cancel setTitle:@"取消" forState:UIControlStateDisabled];
[cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cancel setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
cancel.titleLabel.font = [UIFont systemFontOfSize:14];
Tips:私有变量是如何取到的,下面给出一段代码(看打印后自己凭命名猜的)
//获取第一个成员变量的地址
Ivar *ivars = class_copyIvarList([UISearchBar class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = *(ivars + i);
NSLog(@"%s",ivar_getName(ivar));
}
//谁copy谁释放
free(ivars);