因为Search Bar的游标颜色和Tint颜色是统一的,如果要想将游标的颜色设置为其他颜色,则需要到textfiled内部去修改,对于字体也是同样。
实现代码:
- (void)setSearchBarTextfiled:(UISearchBar *)searchBar{ for (UIView *view in searchBar.subviews){ for (id subview in view.subviews){ if ( [subview isKindOfClass:[UITextField class]] ){ [(UITextField *)subview setTintColor:[UIColor grayColor]]; [(UITextField *)subview setFont:[UIFont fontWithName:GENERAL_FONT size:13]]; return; } } } }
如果要实现用户不能输入到搜索框中,则需要是textfiled的输入失效:
一种方法是对于整个Searchbar设置Disable,但是这样如果有Scope Button等其他组件的话,也会连同失效:
[searchBar setUserInteractionEnabled:NO];
实现代码:
- (void)enableTextInput:(UISearchBar *)searchBar{ for (UIView *view in searchBar.subviews){ for (id subview in view.subviews){ if ( [subview isKindOfClass:[UITextField class]] ){ [subview setEnabled:YES]; return; } } } } - (void)disableTextInput:(UISearchBar *)searchBar{ for (UIView *view in searchBar.subviews){ for (id subview in view.subviews){ if ( [subview isKindOfClass:[UITextField class]] ){ [subview setEnabled:NO]; return; } } } }
参考链接:
http://stackoverflow.com/questions/9968595/how-to-enable-cancel-button-with-uisearchbar
http://stackoverflow.com/questions/11606007/change-uitextfield-and-uitextview-cursor-caret-color
http://stackoverflow.com/questions/2190352/change-color-of-cursor-in-text-field
http://stackoverflow.com/questions/9169049/disable-uisearchbar
时间: 2024-11-04 14:21:44