session-ios/SessionUIKit/Components/SearchBar.swift

45 lines
2.4 KiB
Swift
Raw Normal View History

2020-11-09 06:03:59 +01:00
import UIKit
2019-11-29 06:30:01 +01:00
2020-11-09 06:03:59 +01:00
public final class SearchBar : UISearchBar {
2019-11-29 06:30:01 +01:00
2020-11-09 06:03:59 +01:00
public override init(frame: CGRect) {
2019-11-29 06:30:01 +01:00
super.init(frame: frame)
2019-12-02 01:58:15 +01:00
setUpStyle()
2019-11-29 06:30:01 +01:00
}
2020-11-09 06:03:59 +01:00
public required init?(coder: NSCoder) {
2019-11-29 06:30:01 +01:00
super.init(coder: coder)
2019-12-02 01:58:15 +01:00
setUpStyle()
2019-11-29 06:30:01 +01:00
}
2019-12-02 01:58:15 +01:00
private func setUpStyle() {
2019-11-29 06:30:01 +01:00
searchBarStyle = .minimal // Hide the border around the search bar
barStyle = .black // Use Apple's black design as a base
tintColor = Colors.accent // The cursor color
2020-11-09 06:03:59 +01:00
let searchImage = #imageLiteral(resourceName: "searchbar_search").withTint(Colors.searchBarPlaceholder)!
2019-11-29 06:30:01 +01:00
setImage(searchImage, for: .search, state: .normal)
2020-11-09 06:03:59 +01:00
let clearImage = #imageLiteral(resourceName: "searchbar_clear").withTint(Colors.searchBarPlaceholder)!
2019-11-29 06:30:01 +01:00
setImage(clearImage, for: .clear, state: .normal)
let searchTextField: UITextField
if #available(iOS 13, *) {
searchTextField = self.searchTextField
} else {
searchTextField = self.value(forKey: "_searchField") as! UITextField
}
2019-11-29 06:30:01 +01:00
searchTextField.backgroundColor = Colors.searchBarBackground // The search bar background color
searchTextField.textColor = Colors.text
searchTextField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search", comment: ""), attributes: [ .foregroundColor : Colors.searchBarPlaceholder ])
searchTextField.keyboardAppearance = .dark
setPositionAdjustment(UIOffset(horizontal: 4, vertical: 0), for: UISearchBar.Icon.search)
searchTextPositionAdjustment = UIOffset(horizontal: 2, vertical: 0)
setPositionAdjustment(UIOffset(horizontal: -4, vertical: 0), for: UISearchBar.Icon.clear)
searchTextField.removeConstraints(searchTextField.constraints)
searchTextField.pin(.leading, to: .leading, of: searchTextField.superview!, withInset: Values.mediumSpacing + 3)
searchTextField.pin(.top, to: .top, of: searchTextField.superview!, withInset: 10)
searchTextField.superview!.pin(.trailing, to: .trailing, of: searchTextField, withInset: Values.mediumSpacing + 3)
searchTextField.superview!.pin(.bottom, to: .bottom, of: searchTextField, withInset: 10)
searchTextField.set(.height, to: Values.searchBarHeight)
searchTextField.set(.width, to: UIScreen.main.bounds.width - 2 * Values.mediumSpacing)
}
}