How can I make UISearchBar look this way?

I want to have a search bar like this in my app:

While not editing

While editing

As you can see, there should be magnifying glass to the right while not editing, and clear button while editing. No cancel button.

I created CustomSearchBar class by subclassing UISearchBar:

import UIKit

class CustomSearchBar: U§ISearchBar {

    override func layoutSubviews() {
        super.layoutSubviews()

        customize()
    }

    private func customize() {
        setShowsCancelButton(false, animated: false)

        placeholder = nil
        barTintColor = UIColor.whiteColor()
        tintColor = UIColor.blackColor()

        let textField = getTextField()
        textField.backgroundColor = UIColor.clearColor();
        textField.font = textField.font?.fontWithSize(18.0)

        textField.leftViewMode = .UnlessEditing
    }

}
and UISearchController:

import UIKit

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let result = CustomSearchBar(frame: CGRectZero)
        result.delegate = self

        return result
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }

}

This way, I got proper font size, background colors and no cancel button.

The problem is, I can’t figure out how to make this magnifying glass stay to the right of text field while not editing. I tried this and it haven’t worked. Another issue is, that when I make cancel button disappear using setShowsCancelButton(false, animated: false) it also makes text cursor disappear.

Is it possible, to have bigger clear and magnifying glass icons like on the mockups?

@h4ckmac. Thanks very much for your question, and my apologies for the delayed response. Very detailed and specific technical questions like this are somewhat difficult to answer in these forums. In all honesty, what I would recommend is to post this same question on www.stackoverflow.com, and I can almost guarantee you that you will get a very detailed answer from someone within 24 hours. :slight_smile:

I hope this helps.

All the best!