[英]Unable to change UISearchBar cancel button title color after changing it's text.
我正在使用此代碼更改UISearchBar取消按鈕標題:
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIView* view=_otsinguRiba.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
if (cancelButton) {
[cancelButton setTitle:@"Test") forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
雖然更改文本工作正常,但更改顏色不會。 它保持黑色。
我在一段時間后找到了答案,但我不記得在哪里。 這是我用來設置文本顏色的代碼。
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],NSForegroundColorAttributeName,
//[UIColor whiteColor],UITextAttributeTextShadowColor,
//[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
您可以利用iOS運行時屬性_cancelButton
來實現此目的。
UIButton *cancelButton = [self.searchDisplayController.searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.