Search bar hides on tap - ios

Hi I have added search bar on UIView. When i run my code I can see my search bar, but as I tap inside search bar it hides itself, and when i tap again somewhere on screen it is visible.I am not getting this issue now.Please help.
var searchView:UIView = {
var search = UIView()
search.translatesAutoresizingMaskIntoConstraints = false
search.backgroundColor = UIColor.gray
return search
}()
lazy var searchController : UISearchController = {
var searchController = UISearchController(searchResultsController: nil)
//searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.barTintColor = UIColor.gray
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = UIColor.gray.cgColor
//searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
return searchController
}()
func setUpView(){
view.addSubview(searchView)
searchView.addSubview(searchController.searchBar)
searchView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
searchView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
searchView.topAnchor.constraint(equalTo: view.topAnchor,constant:64).isActive = true
searchView.heightAnchor.constraint(equalToConstant: 65).isActive = true
searchController.searchBar.leftAnchor.constraint(equalTo: searchView.leftAnchor).isActive = true
searchController.searchBar.rightAnchor.constraint(equalTo: searchView.rightAnchor).isActive = true
searchController.searchBar.topAnchor.constraint(equalTo: searchView.topAnchor,constant:10).isActive = true
searchController.searchBar.widthAnchor.constraint(equalTo: searchView.widthAnchor).isActive = true
}
Also I have given this line in ViewDidLoad()-:
self.extendedLayoutIncludesOpaqueBars = true

I found that UISearchController's searchBar doesn't work well when it's set translatesAutoresizingMaskIntoConstraints = false. As a workaround, I embedded the search bar into a placeholder view with the desired constraints:
let searchBarPlaceholderView = UIView()
searchBarPlaceholderView.addSubview(searchController.searchBar)
searchBarPlaceholderView.translatesAutoresizingMaskIntoConstraints = false
searchBarPlaceholderView.heightAnchor.constraint(equalToConstant: 56).isActive = true
stackView.addArrangedSubview(searchBarPlaceholderView)
Notice, that the searchController.searchBar's translatesAutoresizingMaskIntoConstraints property is left true.

Related

selecting search bar cause tableview and search bar to disappear

I used very similar code for another app and the search bar behaves as it should. For some reason, this code below does not work with my current app. Searching online did not yield any answers. The delegate functions aren't the cause of it as when I comment out all their code, it results in the exact same behavior. In fact, when I comment out most of this code below, it results in the same behavior. I don't know why tapping on search destroys my ui. Am I missing something? I must be...
private func configureSearchBar() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.showsCancelButton = true
searchController.searchBar.delegate = self
searchController.searchBar.isUserInteractionEnabled = true
definesPresentationContext = true
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(searchController.searchBar)
searchController.searchBar.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
searchController.searchBar.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true
searchController.searchBar.heightAnchor.constraint(equalToConstant: 55).isActive = true
searchController.searchBar.topAnchor.constraint(equalTo: agendaLabel.bottomAnchor, constant: 8).isActive = true
let color:UIColor = .journeyGold
let lightGold = color.withAlphaComponent(0.5)
searchController.searchBar.tintColor = lightGold
searchController.searchBar.barTintColor = lightGold
searchController.searchBar.backgroundColor = lightGold
searchController.searchBar.layer.borderColor = lightGold.cgColor
navigationItem.hidesSearchBarWhenScrolling = false
searchController.hidesNavigationBarDuringPresentation = true
searchController.searchBar.text = ""
searchController.searchBar.setShowsCancelButton(true, animated: false)
}

How do I move my UIView bottom anchor right about my bottom navigation bar?

I have a controller that will display UIView that displays a text field at the bottom, but I already have a navigation bar down there so whenever I run the simulator, the navigation bar covers it. How would I move that UIView right above my navigation bar? I was messing around with the anchors but I am still having trouble. I was messing around with the containerView.bottomAnchor.
import UIKit
class ChatLogController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Chat Log Controller"
collectionView?.backgroundColor = UIColor.white
setUpInputComponents()
}
func setUpInputComponents() {
let containerView = UIView()
containerView.backgroundColor = UIColor.red
containerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 50).isActive = true
let sendButton = UIButton(type: .system)
sendButton.setTitle("Send", for: .normal)
sendButton.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(sendButton)
sendButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
sendButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
sendButton.widthAnchor.constraint(equalToConstant: 80).isActive = true
sendButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
let inputTextField = UITextField()
inputTextField.placeholder = "Enter message..."
inputTextField.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(inputTextField)
inputTextField.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
inputTextField.rightAnchor.constraint(equalTo: sendButton.leftAnchor).isActive = true
inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
let seperatorLineView = UIView()
seperatorLineView.backgroundColor = UIColor.black
seperatorLineView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(seperatorLineView)
seperatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
seperatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
seperatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
seperatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
}
}
https://imgur.com/a/Cm2jlsy
Set tabBarController.tabBar.translucent to true to make your tabbar opaque.
tabBarController.tabBar.opaque = true
tabBarController.tabBar.translucent = true

Navigating between view controller with search controller search bar hide and show having ui glitch

I have used simple UISearchController in UITableViewController. This has issue when I use search bar in first controller and which is hidden in next or back forth it shows UI glitch.
Here is how it looks>>
Here is below code for setting up navigation bar.
self.extendedLayoutIncludesOpaqueBars = true
let navigationBar = navigationController.navigationBar
let navigationBarTitleTextAttritbutes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.titleTextAttributes = navigationBarTitleTextAttritbutes
if #available(iOS 11.0, *) {
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.prefersLargeTitles = false
}
navigationBar.isTranslucent = false
navigationBar.tintColor = UIColor.white
navigationBar.barTintColor = UIColor.purple
And For setting search controller used as below
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.hidesBottomBarWhenPushed = true
self.searchController.obscuresBackgroundDuringPresentation = false
let searchBarObj = self.searchController.searchBar
searchBarObj.delegate = self as? UISearchBarDelegate
searchBarObj.placeholder = "Search here"
searchBarObj.isTranslucent = false
searchBarObj.tintColor = .white
if #available(iOS 11.0, *) {
searchBarObj.tintColor = .white
searchBarObj.barTintColor = .white
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
let tintColor = UIColor.purple
searchBarObj.barTintColor = tintColor
searchBarObj.layer.borderColor = tintColor.cgColor
searchBarObj.layer.borderWidth = 1
if let table = tableview {
table.tableHeaderView = searchController.searchBar
}
}

Constraint are not working with UINavigationBar item

I am trying to give leading and trailing space to UISearchbar. I have almost tried everything. Height, width, leading and trailing contraint but nothing works. Please let me know how to apply the constraint as I am loading the navigation bar items programmatically.
func setupsearchbar() {
// Setup the Search Controller
searchcontroller.searchResultsUpdater = self as? UISearchResultsUpdating
searchcontroller.hidesNavigationBarDuringPresentation = false
searchcontroller.searchBar.placeholder = "Search"
self.navigationItem.titleView = searchcontroller.searchBar
searchcontroller.searchBar.delegate = self
searchcontroller.searchBar.translatesAutoresizingMaskIntoConstraints = false
searchcontroller.searchBar.widthAnchor.constraint(lessThanOrEqualToConstant: 100.0).isActive = true
if let textfield = searchcontroller.searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.cornerRadius = 18
backgroundview.clipsToBounds = true;
}
}
}
Please help me why my navigation bar is not responding to the Constraint.
Maybe you loss this: self.view.clipsToBounds = true
backgroundview.layer.cornerRadius = 18
backgroundview.clipsToBounds = true;
self.view.clipsToBounds = true

safeAreaLayoutGuide to my inputAccesoryView

So with the new iPhone X, some things in my app are in the wrong position. In the bottom of my app, i have an accesoryView, which is basically an UIView with a textfield and other elements. I saw something about safeAreaLayoutGuide in the new iPhone X, but i do not now how to implement in the accessoryView. So i'm trying to find a code to implement it in my app, so the safeArea does not bother me anymore.
This is the code for the inputAccesoryView
lazy var inputContainerView: UIView = {
let containerView = UIView()
containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = UIColor.white
containerView.addSubview(self.inputTextField)
containerView.addSubview(self.swiche)
containerView.addSubview(self.separatorLineView)
containerView.addSubview(self.uploadImageView)
//x,y,w,h
self.inputTextField.leftAnchor.constraint(equalTo: self.swiche.rightAnchor, constant: 4).isActive = true
self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
self.inputTextField.rightAnchor.constraint(equalTo: self.uploadImageView.leftAnchor, constant: 4).isActive = true
self.inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
self.inputTextField.backgroundColor = UIColor.clear
//x,y,w,h
self.swiche.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 4).isActive = true
self.swiche.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
self.swiche.heightAnchor.constraint(equalToConstant: 30).isActive = true
self.swiche.widthAnchor.constraint(equalToConstant: 55).isActive = true
//x,y,w,h
self.uploadImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
self.uploadImageView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
self.uploadImageView.widthAnchor.constraint(equalToConstant: 47).isActive = true
self.uploadImageView.heightAnchor.constraint(equalToConstant: 47).isActive = true
//x,y,w,h
self.separatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
self.separatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
self.separatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
self.separatorLineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
return containerView
}()
//MARK: AccesoryView
override var inputAccessoryView: UIView? {
get {
return inputContainerView
}
}
Thanks for the help!!!
Just what I thought, all you need to do is accessing safeAreaLayoutGuide class before pointing out the constraint. In your case, you need to change constraints like these:
self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
Into constraint like these:
self.inputTextField.centerYAnchor.constraint(equalTo: containerView.safeAreaLayoutGuide.centerYAnchor).isActive = true
Let me know how it goes.
ok, paste this code before lazy var
override func didMoveToWindow() {
super.didMoveToWindow()
if #available(iOS 11.0, *) {
if let window = self.window {
self.bottomAnchor.constraintLessThanOrEqualToSystemSpacingBelow(window.safeAreaLayoutGuide.bottomAnchor, multiplier: 1.0).isActive = true
}
}
}
now your view is up safeAreaLayoutGuide...but at the bottom you can see the tableview because there is no background (your view is up safeAreaLayoutGuide), for correct the problem I built a white uiview, presented it in inputTextField and set the constraint:
let dummyView = UIView()
dummyView.backgroundColor = .white
dummyView.translatesAutoresizingMaskIntoConstraints = false
Now set the constraint:
inputTextField.addSubview(dummyView)
dummyView.topAnchor.constraint(equalTo: inputTextField.bottomAnchor).isActive = true
dummyView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
dummyView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
dummyView.heightAnchor.constraint(equalToConstant: 50).isActive = true
This is the hack, but I think that's Xcode bug... I hope this help you...
I hope that for your code you simply add this bottom constraint for inputTextField and in other elements is needed, and set containerView CGRect frame height to 100:
self.inputTextField.bottomAnchor.constraint(equalTo: containerView.safeAreaLayoutGuide.bottomAnchor).isActive = true
and I suppose that you can delete:
self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true

Resources