iOS dev iOS dev - 1 year ago 188
Objective-C Question

How to set UiSearchBar Search icon on the right side in swift

I am working on Swift.

As regular SearchBar By default its appearing like Following Pic(1).
enter image description here

But as per our design requirement I need to change it as Following Pic(2).

enter image description here

I tried using the following code I got like Following Pic(3)

enter image description here

**When I tried the following code the right view is getting hide and I could not able to see the text field rightview

Search icon completely invisible**

let searchField = (searchBar.valueForKey("_searchField") as! UITextField)
searchField.layer.cornerRadius = 15;

let searchIcon = searchField.leftView!
if (searchIcon is UIImageView) {

searchField.rightView = searchIcon
searchField.leftViewMode = .Never
searchField.rightViewMode = .Always

Can anyone help me to get the requirement. I can understand if you provide even on objective C as well

Answer Source

In ViewController, leftview of searchbar textfield is the actual search icon, so make it to nil and create a image view with custom icon and set as a right view of search textfield.

Objective C

@interface FirstViewController ()
    UISearchBar  *searchBar;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    searchBar = [[UISearchBar alloc] init];
    searchBar.frame = CGRectMake(15, 100, 350,50);

    [self.view addSubview:searchBar];

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UITextField *searchTextField = [((UITextField *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
    searchTextField.layer.cornerRadius = 15.0f;
    searchTextField.textAlignment = NSTextAlignmentLeft;

    UIImage *image = [UIImage imageNamed:@"search"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    searchTextField.leftView = nil;

    searchTextField.placeholder = @"Search";

    searchTextField.rightView = imageView;
    searchTextField.rightViewMode = UITextFieldViewModeAlways;


    private var searchBar: UISearchBar!

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

        searchBar = UISearchBar()
        searchBar.frame = CGRect(x: 15, y: 100, width: 350, height: 50)


    override func viewDidAppear(_ animated: Bool) {

        let searchTextField:UITextField = searchBar.subviews[0].subviews.last as! UITextField
        searchTextField.layer.cornerRadius = 15
        searchTextField.textAlignment = NSTextAlignment.left
        let image:UIImage = UIImage(named: "search")!
        let imageView:UIImageView = UIImageView.init(image: image)
        searchTextField.leftView = nil
        searchTextField.placeholder = "Search"
        searchTextField.rightView = imageView
        searchTextField.rightViewMode = UITextFieldViewMode.always

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download