I want to have a search bar like this in my app:
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?