我在 UISearchBar 中遇到了 iOS 的问题。我正在使用以下代码来自定义 UISearchBar :
- (void)showSearchBarCustomColorAndDesignWithTargetForiOS7:(id)target{
UIView * subview;
NSArray * subviews = [self subviews];
for (subview in subviews){
for (UIView *level2SubView in subview.subviews){
if ([level2SubView isKindOfClass:[UITextField class]])
{
[((UITextField*)level2SubView) setEnablesReturnKeyAutomatically:NO];
((UITextField*)level2SubView).delegate=target;
[((UITextField*)level2SubView) setEnabled:TRUE];
((UITextField*)level2SubView).borderStyle = UITextBorderStyleLine;
((UITextField*)level2SubView).textColor = [UIColor whiteColor];
((UITextField*)level2SubView).font =[UIFont fontWithName:@"ProximaNova-Light" size:24.0];
((UITextField*)level2SubView).autocorrectionType = UITextAutocorrectionTypeNo;
((UITextField*)level2SubView).layer.borderWidth = 1.0;
((UITextField*)level2SubView).layer.borderColor = UIColorFromRGB(0x2774A7).CGColor;
[[((UITextField*)level2SubView) valueForKey:@"textInputTraits"] setValue:[UIColor whiteColor] forKey:@"insertionPointColor"];
((UITextField*)level2SubView).autocapitalizationType = UITextAutocapitalizationTypeNone;
break;
}
if ([level2SubView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[level2SubView removeFromSuperview];
}
}
}
self.delegate = target;
[self setImage:[UIImage imageNamed:@"search_white.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"delete_white.png"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
[self setBackgroundColor:UIColorFromRGB(0x2774A7)];
}
每当我第一次单击 UISearchBar 时,它都不会调用searchBarShouldBeginEditing
. 它也没有显示我输入的文本。但是,当我第二次单击时,它的行为正常。此外,它显示了一些黑色补丁。我的搜索栏看起来像这样: