我正在尝试自定义UISearchBar的文本字段。下图显示了我完成一半的工作。
我已经对UISearchBar进行了子类化,并从我的视图控制器中调用它。我想要从文本字段中移除那些深灰色的线条。以下是将UISearchBar添加到视图控制器的实现。
![strong text](https://istack.dev59.com/46m4A.webp)
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。
nil
强制转换仍将是nil
。 - Vignesh PT