Cant change colour of searchBar and it jumps to top of view when selected - swift

I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?

Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}

override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}

Related

How can I change ViewController with tapping an UIImageView?

I created a CollectionView. This view has a userProfileImageView and I want to change view controller to ProfilePage when clicked this user profile image. Try some code but neither present nor push view controller doesn't work to me. Can you help me ?
lazy var userProfileImageView: UIImageView = {
let useriv = UIImageView()
useriv.contentMode = .scaleAspectFill
useriv.clipsToBounds = true
useriv.layer.cornerRadius = 40 / 2
useriv.isUserInteractionEnabled = true
useriv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userImageTap)))
return useriv
}()
#objc func userImageTap() {
print("useriv clicked")
let profilePage = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
}

Searchbar cancel button text is truncated

Having various problems setting up the UISearchBar to work properly. It should be shown in the navigationBar. Using the "old" way setting navigationItem.titleView = searchController.searchBar. As seen on the picture below, the Cancel button text is truncated. Any ideas how to solve this problem? I have tried to set searchController.searchBar.sizeToFit() with no luck.
fileprivate lazy var searchController: UISearchController = {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
sc.searchBar.delegate = self
sc.searchBar.returnKeyType = .done
sc.searchBar.tintColor = UIColor.black
sc.searchResultsUpdater = self
sc.hidesNavigationBarDuringPresentation = false
sc.dimsBackgroundDuringPresentation = false
return sc
} ()

Scope buttons hide under search bar on iOS 11

I have a search controller in my app which according to apple documentation on iOS 11 has to be added to the navigationItem's searchController property. I also have some scope buttons for the searchController. Everything works fine but when I rotate my device to landscape and rotate back to portrait then a wired thing happens. The scope buttons gets hidden under the search field. If I press the cancel button and tap on the search field again then it comes back to the normal state. The bug reproduces everytime only on iOS 11. The searchbar will look like this:
The normal state of the search controller:
This is how I add the searchController:
searchController.searchResultsUpdater = self as UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.tintColor = UIColor.black
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search device"
searchController.searchBar.scopeButtonTitles = ["Device", "Person", "OS version", "Serial"]
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
self.navigationItem.hidesSearchBarWhenScrolling = false
if let textField = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor.white
backgroundView.layer.cornerRadius = 10
backgroundView.clipsToBounds = true
}
}
} else {
tableView.tableHeaderView = searchController.searchBar
}
searchController.searchBar.delegate = self as UISearchBarDelegate

How to adjust constraints with collapsing SearchBar in large iOS11 NavigationBar

How do I adjust the constraint when pushing a new UIViewController when a UISearchController is implemented as self.navigationItem.searchController = search
New controller pushed: SearchBar collapses but leaves View with constrait to top = 0 on the initial position.
(The background of the ViewController is yellow for easy understanding of the matter)
Implementation:
viewDidLoad() {
if #available(iOS 11.0, *) {
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.definesPresentationContext = true
search.hidesNavigationBarDuringPresentation = false
search.searchBar.tintColor = UI.COLOR_WHITE
search.dimsBackgroundDuringPresentation = false
self.navigationItem.hidesSearchBarWhenScrolling = false
self.navigationItem.searchController = search
}
I have tried using self.automaticallyAdjustsScrollViewInsets = false without success and the tableView implements self.contentInsetAdjustmentBehavior = .always
What am I missing? Help is very appreciated.

Set textfield in UISearchBar

I'm trying to add SearchController to UINavigationBar. I'm trying to set UITextField of UISearchBar after back button of UINavigationController. I want some more space after back button
When I start searching it appears as
Whereas I should be able to view back button. Only textfield width should be decreased. Also after Cancel it should be again back to initial layout. Whereas it is displayed as below:
Below is my code
var searchResultController = UISearchController()
self.searchResultController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
controller.hidesNavigationBarDuringPresentation = false
self.navigationController?.navigationBar.addSubview(controller.searchBar)
return controller
})()
override func viewDidAppear(animated: Bool) {
for subView in searchResultController.searchBar.subviews{
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.width = self.view.bounds.width - 50
}
}
}
}
Please let me know how can I fix this.
Thanks in advance
For setting the UIsearchBar add it to Navigation's titleView as
self.navigationItem.titleView = controller.searchBar
For removing Cancel button we can use UISearchControllerDelegate method
func didPresentSearchController(searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
Hope this may help any one.