I'm trying to customize the textfield for the UISearchbar. The picture below shows my half done work.
I have subclasses the UISearchbar and called it from my view controller. I'm trying to remove those dark gray lines from the textfield. Below is the implementation of the UISearchbar adding to subview of the viewcontroller.
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 subclass:
- (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];
}
I'm trying to achive something like this: The textfield should not have any boundaries. The icons are flattened UIImageView.
Using searchbar. heightAnchor. constraint(equalToConstant: 200). isActive = true worked for me.
Calling inside searchBarTextDidBeginEditing() method will show cancel button after clicking on search bar. Show activity on this post. Use this code for showing cancel button.
Change Search Bar Default Image Color The left hand default search image in UISearchBar represents the left view of the UITextField. The Image is rendered to change it to the desired colour. @IBOutlet weak var searchBar: UISearchBar! Hope it will help you in customising the UISearchBar in your app.
iOS 13.x & Swift 5.x:
Do the following to access the UITextField
in UISearchBar
.
extension UISearchBar {
/// Returns the`UITextField` that is placed inside the text field.
var textField: UITextField {
if #available(iOS 13, *) {
return searchTextField
} else {
return self.value(forKey: "_searchField") as! UITextField
}
}
}
For those still looking for an answer, Apple has added the searchTextField
property to UISearchBar in iOS 13. searchTextField
is a UISeachTextField which inherits from UITextField.
let searchBar = UISearchBar()
var searchField : UITextField
if #available(iOS 13.0, *) {
searchField = searchBar.searchTextField
} else {
searchField = //One of the other methods listed
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With