我正在尝试自定义 UISearchbar 的文本字段。下图显示了我完成了一半的工作。
我有子类 UISearchbar 并从我的视图控制器中调用它。我正在尝试从文本字段中删除那些深灰色线条。下面是添加到视图控制器子视图的 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。