[ios] fix search bars for iOS12
Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
parent
f05be9bf71
commit
b9582b9067
2 changed files with 69 additions and 37 deletions
|
@ -24,26 +24,43 @@ extension UISearchBar {
|
|||
class UISearchBarRenderer: UIViewRenderer {
|
||||
class func render(_ control: UISearchBar, style: Style) {
|
||||
super.render(control, style: style)
|
||||
var searchTextField: UITextField?
|
||||
if #available(iOS 13, *) {
|
||||
searchTextField = control.searchTextField
|
||||
}
|
||||
|
||||
// Default search bar implementation adds the grey transparent image for background. This code removes it and updates the corner radius. This is not working on iPad designed for mac.
|
||||
if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
|
||||
var searchTextField = control.searchTextField
|
||||
// Default search bar implementation adds the grey transparent image for background. This code removes it and updates the corner radius. This is not working on iPad designed for mac.
|
||||
if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {
|
||||
} else {
|
||||
control.setSearchFieldBackgroundImage(UIImage(), for: .normal)
|
||||
}
|
||||
searchTextField.layer.setCorner(radius: 8)
|
||||
searchTextField.layer.masksToBounds = true
|
||||
// Placeholder color
|
||||
if let placeholder = searchTextField.placeholder {
|
||||
searchTextField.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [.foregroundColor: UIColor.gray])
|
||||
}
|
||||
if let backgroundColor = style.backgroundColor {
|
||||
searchTextField.backgroundColor = backgroundColor
|
||||
}
|
||||
if let font = style.font {
|
||||
searchTextField.font = font
|
||||
}
|
||||
if let fontColor = style.fontColor {
|
||||
searchTextField.textColor = fontColor
|
||||
}
|
||||
if let tintColor = style.tintColor {
|
||||
searchTextField.leftView?.tintColor = tintColor
|
||||
// Placeholder indicator color
|
||||
searchTextField.tintColor = tintColor
|
||||
// Clear button image
|
||||
let clearButtonImage = UIImage(named: "ic_clear")?.withRenderingMode(.alwaysTemplate).withTintColor(tintColor)
|
||||
control.setImage(clearButtonImage, for: .clear, state: .normal)
|
||||
}
|
||||
} else {
|
||||
control.setSearchFieldBackgroundImage(UIImage(), for: .normal)
|
||||
}
|
||||
searchTextField?.layer.setCorner(radius: 8)
|
||||
searchTextField?.layer.masksToBounds = true
|
||||
|
||||
// Placeholder color
|
||||
if let placeholder = searchTextField?.placeholder {
|
||||
searchTextField?.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [.foregroundColor: UIColor.gray])
|
||||
}
|
||||
|
||||
if let backgroundColor = style.backgroundColor {
|
||||
searchTextField?.backgroundColor = backgroundColor
|
||||
// Default search bar implementation for iOS12 adds the dark grey transparent image for background. This code removes it and replace with the custom image accordingly to the documentation - see 'setSearchFieldBackgroundImage'.
|
||||
if let backgroundColor = style.backgroundColor {
|
||||
let image = getSearchBarBackgroundImage(color: backgroundColor)
|
||||
control.setSearchFieldBackgroundImage(image, for: .normal)
|
||||
control.searchTextPositionAdjustment = UIOffset(horizontal: 6.0, vertical: 0.0)
|
||||
}
|
||||
}
|
||||
if let barTintColor = style.barTintColor {
|
||||
let position = control.delegate?.position?(for: control) ?? control.barPosition
|
||||
|
@ -51,29 +68,10 @@ class UISearchBarRenderer: UIViewRenderer {
|
|||
control.setBackgroundImage(barTintColor.getImage(), for: position, barMetrics: .default)
|
||||
control.backgroundColor = barTintColor
|
||||
}
|
||||
if let font = style.font {
|
||||
searchTextField?.font = font
|
||||
}
|
||||
if let fontColor = style.fontColor {
|
||||
searchTextField?.textColor = fontColor
|
||||
}
|
||||
if let fontColorDetailed = style.fontColorDetailed {
|
||||
// Cancel button color
|
||||
control.tintColor = fontColorDetailed
|
||||
}
|
||||
if let tintColor = style.tintColor {
|
||||
searchTextField?.leftView?.tintColor = tintColor
|
||||
// Placeholder indicator color
|
||||
searchTextField?.tintColor = tintColor
|
||||
// Clear button image
|
||||
let clearButtonImage: UIImage?
|
||||
if #available(iOS 13.0, *) {
|
||||
clearButtonImage = UIImage(named: "ic_clear")?.withRenderingMode(.alwaysTemplate).withTintColor(tintColor)
|
||||
} else {
|
||||
clearButtonImage = UIImage(named: "ic_search_clear_14")
|
||||
}
|
||||
control.setImage(clearButtonImage, for: .clear, state: .normal)
|
||||
}
|
||||
}
|
||||
|
||||
//fix for iOS 12 and below
|
||||
|
@ -86,10 +84,43 @@ class UISearchBarRenderer: UIViewRenderer {
|
|||
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = font
|
||||
}
|
||||
if let fontColor = style.fontColor {
|
||||
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [.foregroundColor: fontColor]
|
||||
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = fontColor
|
||||
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).leftView?.tintColor = fontColor
|
||||
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = fontColor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS, deprecated: 13.0)
|
||||
private static let kiOS12DefaultSystemTextFieldHeight = 36
|
||||
|
||||
@available(iOS, deprecated: 13.0)
|
||||
private static var searchBarBackgroundColor: UIColor?
|
||||
|
||||
@available(iOS, deprecated: 13.0)
|
||||
private static var searchBarBackgroundImage: UIImage?
|
||||
|
||||
// Draws the background image for the UITextField using the default system's text field height.
|
||||
// This approach is used only for iOS 12.
|
||||
@available(iOS, deprecated: 13.0)
|
||||
private static func getSearchBarBackgroundImage(color: UIColor) -> UIImage? {
|
||||
if color != searchBarBackgroundColor {
|
||||
let size = CGSize(width: kiOS12DefaultSystemTextFieldHeight, height: kiOS12DefaultSystemTextFieldHeight)
|
||||
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
|
||||
guard let context = UIGraphicsGetCurrentContext() else { return nil }
|
||||
let rect = CGRect(origin: .zero, size: size)
|
||||
let cornerRadius = CGFloat(8)
|
||||
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: [.topLeft, .topRight, .bottomLeft, .bottomRight], cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
|
||||
context.addPath(path.cgPath)
|
||||
context.setFillColor(color.cgColor)
|
||||
context.fillPath()
|
||||
let image = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
searchBarBackgroundImage = image
|
||||
searchBarBackgroundColor = color
|
||||
return image
|
||||
}
|
||||
return searchBarBackgroundImage
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ final class SearchBar: SolidTouchView {
|
|||
updateLeftView()
|
||||
searchTextField.leftViewMode = UITextField.ViewMode.always
|
||||
searchTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
|
||||
searchTextField.applyTheme()
|
||||
}
|
||||
|
||||
private func updateLeftView() {
|
||||
|
|
Reference in a new issue