自定义UISearchbar的UITextfield – iOS

我正在尝试自定义UISearchbar的文本字段。 下面的图片显示了我的一半完成的工作。 强壮的文字

我有UISearchbar的子类,并从我的视图控制器调用它。 我正试图从文本字段中删除那些黑灰色的线条。 下面是添加到viewcontroller的子视图的UISearchbar的实现。

searchbar = [[SearchBar alloc] initWithFrame:CGRectMake(35,78, 250, 17)]; searchbar.backgroundColor = [UIColor clearColor]; searchbar.layer.borderColor = [[UIColor clearColor] CGColor]; searchbar.layer.borderWidth = 0; for(UIView *view in searchbar.subviews){ if([view isKindOfClass:[UITextField class]]){ UITextField *tf= (UITextField *)view; tf.layer.borderColor = [[UIColor clearColor] CGColor]; tf.delegate = self; break; } } [self.view addSubview:searchbar]; searchbar.delegate = self; 

UISearchBar子类:

  - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(void)layoutSubviews{ UITextField *searchField; [[[self subviews] objectAtIndex:0] removeFromSuperview]; [self setTintColor:[UIColor clearColor]]; self.clipsToBounds = YES; NSUInteger numViews = [self.subviews count]; for(int i = 0; i < numViews; i++) { if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { searchField = [self.subviews objectAtIndex:i]; searchField.leftViewMode = UITextFieldViewModeNever; searchField.backgroundColor = [UIColor clearColor]; } } if(!(searchField == nil)) { searchField.backgroundColor = [UIColor clearColor]; searchField.textColor = [UIColor blackColor]; searchField.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height-10); [searchField setBorderStyle:UITextBorderStyleRoundedRect]; } [super layoutSubviews]; } 

我试图达到这样的东西:文本框不应该有任何界限。 图标被压平UIImageView。

在这里输入图像说明

这是从UISearchBar子视图层次结构中获取文本字段的简单方法,并根据需要设置其属性

  UITextField *txfSearchField = [searchbar valueForKey:@"_searchField"]; [txfSearchField setBackgroundColor:[UIColor whiteColor]]; [txfSearchField setLeftView:UITextFieldViewModeNever]; [txfSearchField setBorderStyle:UITextBorderStyleRoundedRect]; txfSearchField.layer.borderWidth = 8.0f; txfSearchField.layer.cornerRadius = 10.0f; txfSearchField.layer.borderColor = [UIColor clearColor].CGColor; 

如果您不想使用未logging的function或使用图像,请使用以下内容:

 CGSize size = CGSizeMake(30, 30); // create context with transparent background UIGraphicsBeginImageContextWithOptions(size, NO, 1); // Add a clip before drawing anything, in the shape of an rounded rect [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,30,30) cornerRadius:2.0] addClip]; [[UIColor whiteColor] setFill]; UIRectFill(CGRectMake(0, 0, size.width, size.height)); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [self.searchBar setSearchFieldBackgroundImage:image forState:UIControlStateNormal]; 

从IOS-5开始,您有外观代理,请参阅: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UISearchBar_Class/Reference/Reference.html (有两个部分称为“自定义外观“,同时检查)。

这是一个工作的例子,它修改了应用程序中的所有UISearchBars:

 [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"text_box"] forState:UIControlStateNormal]; [[UISearchBar appearance] setImage:[UIImage imageNamed:@"search_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; mysearchBar.tintColor = [UIColor whiteColor]; 

实施改变抗性和高性能

任何人读这个,想知道如何在Swift中很容易地访问search栏的文本字段; 你可以扩展UISearchBar来添加一个textField属性,通过使用下面这段代码来抵抗潜在的实现变化并cachingfind的结果,所以它是高性能的。

 import UIKit private var foundTextFieldAssociationKey = UInt8() extension UISearchBar { var textField: UITextField { get { let value = objc_getAssociatedObject(self, &foundTextFieldAssociationKey) as? UITextField if value == nil { let findInView = (UIView) -> UITextField? = { view in for subview in view.subviews { if let textField = (subview as? UITextField) ?? findInView(subview) { return textField } } return nil } guard let foundTextField = findInView(self) else { fatalError("UISearchBar doesn't seem to have a UITextField anywhere in the view hierarchy") } textField = foundTextField return foundTextField } return value! } set { objc_setAssociatedObject(self, &foundTextFieldAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN) } } } 

如果您想更改属性的名称,请确保不要将其更改为searchField 。 这会弄乱你的search栏。

我已经find了iOS 10我需要做到这一点(从上面借来,很快适应)

 extension UISearchBar { var textField: UITextField? { func findInView(_ view: UIView) -> UITextField? { for subview in view.subviews { print("checking \(subview)") if let textField = subview as? UITextField { return textField } else if let v = findInView(subview) { return v } } return nil } return findInView(self) } } 

简单而简单

 extension UISearchBar { var textField:UITextField { guard let txtField = self.value(forKey: "_searchField") as? UITextField else { assertionFailure() return UITextField() } return txtField } }