Skip to content

Instantly share code, notes, and snippets.

@mukyasa
Last active July 7, 2021 14:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mukyasa/a39dea0dc1019d5f1cc654879577ff8b to your computer and use it in GitHub Desktop.
Save mukyasa/a39dea0dc1019d5f1cc654879577ff8b to your computer and use it in GitHub Desktop.
extension UISearchBar {
public var textField: UITextField? {
if #available(iOS 13, *) {
return searchTextField
}
let subViews = subviews.flatMap { $0.subviews }
guard let textField = (subViews.filter { $0 is UITextField }).first as? UITextField else {
return nil
}
return textField
}
func clearBackgroundColor() {
guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }
for view in subviews {
for subview in view.subviews where subview.isKind(of: UISearchBarBackground) {
subview.alpha = 0
}
}
}
public var activityIndicator: UIActivityIndicatorView? {
return textField?.leftView?.subviews.compactMap { $0 as? UIActivityIndicatorView }.first
}
var isLoading: Bool {
get {
return activityIndicator != nil
} set {
if newValue {
if activityIndicator == nil {
let newActivityIndicator = UIActivityIndicatorView(style: .gray)
newActivityIndicator.color = UIColor.gray
newActivityIndicator.startAnimating()
newActivityIndicator.backgroundColor = textField?.backgroundColor ?? UIColor.white
textField?.leftView?.addSubview(newActivityIndicator)
let leftViewSize = textField?.leftView?.frame.size ?? CGSize.zero
newActivityIndicator.center = CGPoint(x: leftViewSize.width - newActivityIndicator.frame.width / 2,
y: leftViewSize.height / 2)
}
} else {
activityIndicator?.removeFromSuperview()
}
}
}
func changePlaceholderColor(_ color: UIColor) {
guard let UISearchBarTextFieldLabel: AnyClass = NSClassFromString("UISearchBarTextFieldLabel"),
let field = textField else {
return
}
for subview in field.subviews where subview.isKind(of: UISearchBarTextFieldLabel) {
(subview as! UILabel).textColor = color
}
}
func setRightImage(normalImage: UIImage,
highLightedImage: UIImage) {
showsBookmarkButton = true
if let btn = textField?.rightView as? UIButton {
btn.setImage(normalImage,
for: .normal)
btn.setImage(highLightedImage,
for: .highlighted)
}
}
func setLeftImage(_ image: UIImage,
with padding: CGFloat = 0,
tintColor: UIColor) {
let imageView = UIImageView()
imageView.image = image
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 20).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 20).isActive = true
imageView.tintColor = tintColor
if padding != 0 {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.alignment = .center
stackView.distribution = .fill
stackView.translatesAutoresizingMaskIntoConstraints = false
let paddingView = UIView()
paddingView.translatesAutoresizingMaskIntoConstraints = false
paddingView.widthAnchor.constraint(equalToConstant: padding).isActive = true
paddingView.heightAnchor.constraint(equalToConstant: padding).isActive = true
stackView.addArrangedSubview(paddingView)
stackView.addArrangedSubview(imageView)
textField?.leftView = stackView
} else {
textField?.leftView = imageView
}
}
}
extension UIImage {
convenience init(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage!)!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment