UISearchBar magnifying glass icon goes outside searchbar - ios

When searchbar is in inactive state and the placeholder is too big, the magnifying glass icon goes outside the searchbar. Anyone knows how to fix this? Nothing
lazy var searchBar: UISearchBar = {
let searchBar = UISearchBar()
searchBar.autocorrectionType = .yes
searchBar.autocapitalizationType = .none
searchBar.placeholder = ChangeToLanguage(SEARCH_LABEL_PLACEHOLDER_SEARCHMODULES)
searchBar.delegate = self
searchBar.isTranslucent = false
let searchBarTextField = searchBar.value(forKey: "searchField") as? UITextField
searchBarTextField?.tintColor = GlobalTintColor
searchBar.barTintColor = GlobalTintColor
return searchBar
}()

Try adjusting the position of search bar icon by grabbing the UITextField attached to UISearchBar as mentioned in this post.
https://stackoverflow.com/a/36594565/2740582

Related

XCODE: How do I hide the cursor from my searchBar after the user is done searching?

I have looked tirelessly through SO and haven't found out how to do this. How do I hide the cursor from my searchBar after the user is done searching? It just keeps blinking when the user doesn't need it to.
For normal UITextFields, if you set the tintColor property to .clear, the cursor will hide.
Similarly, for a UISearchBar, I would try
let sb = UISearchBar()
sb.searchTextField.tintColor = .clear
OR
let sb = UISearchBar()
sb.tintColor = .clear
Let me know if it works.

How can i change the color of placeholder in SearchBar in Swift Xcode

I have this design of the search bar and the output is not the same.
I have tried TintColor, BackgroundColor, Different Styles in addition to many extensions on the internet and yet i still cannot achieve it.
All what i have so far is the normal searchbar with the grey area color at the placeholder.
To change the textColor for placeholder in SearchBar you need to extract the searchField from searchBar and then placeholderLabel from searchField. Here's how:
let searchField = searchBar.value(forKey: "searchField") as? UITextField
searchField?.textColor = myColor
let placeholderLabel = searchField?.value(forKey: "placeholderLabel") as? UILabel
placeholderLabel?.textColor = myColor

How make UISearchbar rectangular shape and merge with navigation bar?

Hello I'm trying to change the UISearchbar to rectangular shape rather then rounded rectangular and merge it with the navigation bar with same color.
The background color of the navigation bar should be the same as the background of the search bar but there's difference and also there's a clear edge shown both should show the background color #be1b25 and not the #bc303a. As well the textfield still showing rounded rectangle and not fully rectangle.
The Main View Controller code
#IBOutlet weak var searchBar: UISearchBar!
self.navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.backgroundColor = UIColor().HexToColor(hexString: REDCOLOR)
searchBar.backgroundColor = UIColor().HexToColor(hexString: REDCOLOR)
searchBar.tintColor = UIColor().HexToColor(hexString: REDCOLOR)
searchBar.barTintColor = UIColor().HexToColor(hexString: REDCOLOR)
searchBar.barStyle = .default
searchBar.placeholder = NetworkManager.sharedInstance.language(key: "searchentirestore")
searchBar.layer.cornerRadius = 0;
searchBar.clipsToBounds = false;
searchBar.barTintColor = UIColor().HexToColor(hexString: REDCOLOR)
searchBar.layer.borderWidth = 0
//searchBar.layer.borderColor = UIColor.clear as! CGColor
The UISearchBar Extension
extension UISearchBar {
private var textField: UITextField? {
return subviews.first?.subviews.flatMap { $0 as? UITextField }.first
}
private var activityIndicator: UIActivityIndicatorView? {
return textField?.leftView?.subviews.flatMap{ $0 as? UIActivityIndicatorView }.first
}
var isLoading: Bool {
get {
return activityIndicator != nil
} set {
if newValue {
if activityIndicator == nil {
let newActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
newActivityIndicator.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
newActivityIndicator.startAnimating()
newActivityIndicator.color = UIColor().HexToColor(hexString: BUTTON_COLOR)
newActivityIndicator.backgroundColor = UIColor.white
textField?.leftView?.addSubview(newActivityIndicator)
let leftViewSize = textField?.leftView?.frame.size ?? CGSize.zero
newActivityIndicator.center = CGPoint(x: leftViewSize.width/2, y: leftViewSize.height/2)
}
} else {
activityIndicator?.removeFromSuperview()
}
}
}
Any help please, this is Swift 3. Thanks in advance
so there a few things you are dealing with
1 Rectangular UISearchbar
either you will traverse subviews of native UISearchbar and change its right sublayers corner radiuses, but this is a hack, Apple can change that hierarchy and you will be at the beginning again
you can create your own component that will look like UISearchbar, few UIViews, and UITextfield with some UIImageViews.
there are few libraries on GitHub you can use https://github.com/CosmicMind/Samples
there is also a similar question here iOS 11 UISearchBar in UINavigationBar
2 Merge with NavBar
one way is to use UISearchController with iOS 11
or you can with your custom UISearchbar you can either add it below the NavBar in UIView, not in navigation, but the NavBar cannot be translucent in this case, because you will never achieve the same color space
you can add your custom view as custom UIView into titleView in NavBar
Extend the UISearchBar and apply the customization.
Set the UISearchBar as titleView to the NavigationController
class SearchField: UISearchBar {
override init(frame: CGRect) {
super.init(frame: frame)
// Corner radius
self.layer.cornerRadius = 2.0
self..clipsToBounds = true
// Textfield inside color.
if let searchTextField = self.value(forKey: "_searchField") as? UITextField {
// Set TextField property
// Set the corner radius for TextField (not suggested)
if let clearButton = searchTextField.value(forKey: "_clearButton") as? UIButton {
// Set the clear button property
}
}
}
}
// Use it in the ViewController.
let searchBar = SearchField()
self.navigationItem.titleView = searchBar

ios swift search bar color change

I am trying to change the gray area (rectangle area) color of the search bar to a different color (orange). Ive tried using searchBar.barTintColor = but that is only changing the color of the cancel button and not the gray area.
EDIT
class usersVC: UITableViewController, UISearchBarDelegate {
// declare searchBar
var searchBar = UISearchBar()
// default function
override func viewDidLoad() {
super.viewDidLoad()
// implement search bar
searchBar.delegate = self
searchBar.showsCancelButton = true
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.frame.size.width = self.view.frame.size.width - 30
let searchItem = UIBarButtonItem(customView: searchBar)
self.navigationItem.leftBarButtonItem = searchItem
}
Try this again.
I created this effect by loading a simple image with an orange background, seen below.
searchBar.backgroundImage = UIImage(named: "orange")
Background image:
EDIT 1
Based on your recent comment, I tested this with a programatically created searchbar and it works as well.

UISearchBar custom corners

I'm trying to create a search bar like this:
But I'm noticing that I'm probably going to have to replace the search bar with my own image because the search bar corners comes out wrong when I set:
self.searchController.searchBar.layer.cornerRadius = 50 // I've tried other numbers besides this too with no luck
self.searchController.searchBar.clipsToBounds = true
If I set this:
self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.bounds.height/2
The search bar comes out like this:
Which still isn't exact like in the image.
Is there a way to replace the left and right side of the textfield with an image that way I can use the rounded corners from my custom search bar?
I am using this code UISearchBar but you can use this code with UISearchController.
let searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 14;
backgroundview.clipsToBounds = true;
}
}
You should change the radius of searchTextField inside UISearchBar .
you can do that like this :
searchBar.searchTextField.layer.cornerRadius = 20
searchBar.searchTextField.layer.masksToBounds = true
* searchBar is an outlet of UISearchBar from storyBoard
The issue here is you are setting the corner radius on the UISearchBar, not the UITextField inside it. You can do some sort of hack to get the UITextField, but that's not really recommended.
As you mentioned in your question, you'll need to use custom images and the methods shown here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40
This IS working for me in swift 3 iOS 10:
searchController.searchBar.layer.cornerRadius = 20
searchController.searchBar.clipsToBounds = true
ez way for searchbarview
for subview & POPUPs [Swift 5]
override func layoutSublayers(of layer: CALayer) {
searchBarPopup.clipsToBounds = true
searchBarPopup.layer.cornerRadius = 10
searchBarPopup.layer.maskedCorners = [ .layerMaxXMinYCorner, .layerMinXMinYCorner]
}

Resources