Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize UITextfield of the UISearchbar - iOS

I'm trying to customize the textfield for the UISearchbar. The picture below shows my half done work. strong text

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.

enter image description here

like image 238
DesperateLearner Avatar asked Oct 19 '12 04:10

DesperateLearner


People also ask

How do I change the height of a search bar in Swift?

Using searchbar. heightAnchor. constraint(equalToConstant: 200). isActive = true worked for me.

How do I add a cancel button to my search bar?

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.

How do I change the search bar color in Swift?

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.


2 Answers

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
        }
    }

}
like image 190
Baran Emre Avatar answered Nov 10 '22 20:11

Baran Emre


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
}
like image 35
Luke Redmore Avatar answered Nov 10 '22 18:11

Luke Redmore