selecting search bar cause tableview and search bar to disappear - ios

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)
}

Related

Swift View is not hiding subViews correctly

UPDATE AT THE BOTTOM
Inside my ViewController I have a TableView with CustomCells. The content being presented in those cells is depending on the userInput. I think the best way to explain the problem is to actually show it:
1. adding cells to the tableView:
looking as expected
2. Problem: after dismissing the ViewController and going back to it:
showing views that should actually be hidden in the first cell
By the way, when I open the View-Hirarchy in the Debugger, it is being displayed correctly !
Here is also another video for a better understanding: video
In this case I didn't add an image, but when going back to the viewController it still shows the imageContainerView(shadow) and also the content for the first cell.
Code:
My code is quite complex and messy so I you can follow me here:
setupViews in CustomCell:
I don't think that this is quite helpful, but I also don't think that the setup is the issue here.
func setupViews(){
contentView.addSubview(checkButton)
contentView.addSubview(mainStackView)
// main StackView
mainStackView.addArrangedSubview(label)
mainStackView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
mainStackView.leadingAnchor.constraint(equalTo: checkButton.trailingAnchor, constant: 15).isActive = true
mainStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -30).isActive = true
mainStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
//constrain wish label
labelHeightConatraint = label.heightAnchor.constraint(equalToConstant: 50)
labelHeightConatraint.priority = .defaultHigh
labelHeightConatraint.isActive = true
// constrain checkButton
checkButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 30).isActive = true
checkButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
mainStackView.addArrangedSubview(secondaryStackView)
secondaryStackViewHeightConstraint = secondaryStackView.heightAnchor.constraint(equalToConstant: 90)
secondaryStackViewHeightConstraint.priority = .defaultHigh
secondaryStackViewHeightConstraint.isActive = true
secondaryStackView.addArrangedSubview(imageContainerView)
imageContainerWidthConstraint = imageContainerView.widthAnchor.constraint(equalToConstant: 90)
imageContainerWidthConstraint.priority = .defaultHigh
imageContainerWidthConstraint.isActive = true
imageContainerView.addSubview(shadowLayer)
shadowLayer.widthAnchor.constraint(equalToConstant: 80).isActive = true
shadowLayer.heightAnchor.constraint(equalToConstant: 80).isActive = true
shadowLayer.topAnchor.constraint(equalTo: imageContainerView.topAnchor).isActive = true
shadowLayer.trailingAnchor.constraint(equalTo: imageContainerView.trailingAnchor, constant: -10).isActive = true
imageContainerView.addSubview(wishImage)
wishImage.widthAnchor.constraint(equalToConstant: 80).isActive = true
wishImage.heightAnchor.constraint(equalToConstant: 80).isActive = true
wishImage.topAnchor.constraint(equalTo: imageContainerView.topAnchor).isActive = true
wishImage.trailingAnchor.constraint(equalTo: imageContainerView.trailingAnchor, constant: -10).isActive = true
secondaryStackView.addArrangedSubview(thirdHelperView)
thirdHelperView.addSubview(thirdStackView)
thirdHelperViewHeightConstraint = thirdHelperView.heightAnchor.constraint(equalToConstant: 90)
thirdHelperViewHeightConstraint.priority = .defaultHigh
thirdHelperViewHeightConstraint.isActive = true
thirdStackView.addArrangedSubview(priceView)
priceView.heightAnchor.constraint(equalToConstant: 30).isActive = true
priceView.addSubview(priceImage)
priceView.addSubview(priceLabel)
thirdStackView.addArrangedSubview(linkView)
linkView.heightAnchor.constraint(equalToConstant: 30).isActive = true
linkView.addSubview(linkImage)
linkView.addSubview(linkTextView)
thirdStackView.addArrangedSubview(noteView)
noteView.heightAnchor.constraint(equalToConstant: 30).isActive = true
noteView.addSubview(noteImage)
noteView.addSubview(noteLabel)
priceImage.topAnchor.constraint(equalTo: priceView.topAnchor).isActive = true
priceImage.leadingAnchor.constraint(equalTo: thirdStackView.leadingAnchor).isActive = true
priceImage.heightAnchor.constraint(equalToConstant: 20).isActive = true
priceImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
priceLabel.topAnchor.constraint(equalTo: priceView.topAnchor).isActive = true
priceLabel.leadingAnchor.constraint(equalTo: priceImage.trailingAnchor, constant: 10).isActive = true
priceLabel.trailingAnchor.constraint(equalTo: priceView.trailingAnchor, constant: -10).isActive = true
linkImage.topAnchor.constraint(equalTo: linkView.topAnchor).isActive = true
linkImage.leadingAnchor.constraint(equalTo: thirdStackView.leadingAnchor).isActive = true
linkImage.heightAnchor.constraint(equalToConstant: 20).isActive = true
linkImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
linkTextView.topAnchor.constraint(equalTo: linkView.topAnchor).isActive = true
linkTextView.leadingAnchor.constraint(equalTo: linkImage.trailingAnchor, constant: 10).isActive = true
linkTextView.trailingAnchor.constraint(equalTo: linkView.trailingAnchor, constant: -10).isActive = true
noteImage.topAnchor.constraint(equalTo: noteView.topAnchor).isActive = true
noteImage.leadingAnchor.constraint(equalTo: thirdStackView.leadingAnchor).isActive = true
noteImage.heightAnchor.constraint(equalToConstant: 20).isActive = true
noteImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
noteLabel.topAnchor.constraint(equalTo: noteView.topAnchor).isActive = true
noteLabel.leadingAnchor.constraint(equalTo: noteImage.trailingAnchor, constant: 10).isActive = true
noteLabel.trailingAnchor.constraint(equalTo: noteView.trailingAnchor, constant: -10).isActive = true
}
more importantly: cellForRowAt, where I actually hide/show the different views depending on the content. As you can see I actually call .isHidden on the ImageContainerView, priceView, linkView & noteView , if there content is empty ( which it is in the first cell )
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath) as! WhishCell
cell.label.text = ""
cell.linkTextView.text = ""
cell.priceLabel.text = ""
cell.noteLabel.text = ""
cell.wishImage.image = UIImage()
let currentWish = self.wishData[indexPath.row]
cell.label.text = currentWish.name
cell.linkTextView.hyperLink(originalText: "Link öffnen", hyperLink: "Link öffnen", urlString: currentWish.link)
cell.priceLabel.text = currentWish.price
cell.noteLabel.text = currentWish.note
cell.wishImage.image = currentWish.image
cell.setupSuccessAnimation()
cell.noteView.isHidden = false
cell.priceView.isHidden = false
cell.linkView.isHidden = false
cell.imageContainerView.isHidden = false
cell.secondaryStackViewHeightConstraint.constant = 0
cell.thirdHelperViewHeightConstraint.constant = 0
if currentWish.image == nil || !currentWish.image!.hasContent {
cell.imageContainerView.isHidden = true
print("but its truue: \(cell.imageContainerView.isHidden)")
if currentWish.price != "" {
cell.thirdHelperViewHeightConstraint.constant += 30
cell.secondaryStackViewHeightConstraint.constant += 30
}
if currentWish.link != "" {
cell.thirdHelperViewHeightConstraint.constant += 30
cell.secondaryStackViewHeightConstraint.constant += 30
}
if currentWish.note != "" {
cell.thirdHelperViewHeightConstraint.constant += 30
cell.secondaryStackViewHeightConstraint.constant += 30
}
} else {
cell.secondaryStackViewHeightConstraint.constant = 90
cell.thirdHelperViewHeightConstraint.constant = 90
}
if currentWish.price == "" {
cell.priceView.isHidden = true
}
if currentWish.link == "" {
cell.linkView.isHidden = true
}
if currentWish.note == "" {
cell.noteView.isHidden = true
}
return cell
}
I have no idea why this happens. I don't think something is wrong with the setup as it works if I actually add the cells. It is simply not hiding the views when it actually should. The cellheight is also working as expected. Just the damn hiding...
I know this is a lot but I hope my problem is clear. If you need any thing more just let me know!
Update:
I added two print-statements inside cellForRowAt and is actually printing this:
print("but its truue: \(cell.imageContainerView.isHidden)")
print("but its truue: \(cell.shadowLayer.isHidden)")
but its truue: true
but its truue: false
so it is hiding the imageConatinerView correctly but not shadowLayer even though shadowLayer is a subView of imageContainerView??! Im stuck here...
You can use UIView debugging method to debug your UI.
There are four useful methods to debug:
hasAmbiguousLayout:
exerciseAmbiguityInLayout:
exerciseAmbiguityInLayout:
_autolayoutTrace:
And then you can use Xcode UI Debug to review your UI

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

Adding focus to a UISearchBar in custom UIView programmatically not working

What I am trying to achieve is to have a button click action trigger a uiview popup with a search bar at the top of the view, which then displays a UISearchBar and a UITableView below. I have been successful at implementing these events, but when clicking inside the search bar nothing happens. The search bar just has the word search and a search bar button. I have subclassed the UISearchBarDelegate, UISearchResultsUpdating, and UISearchControllerDelegate for the ViewController. As mentioned, I am doing this programmatically in Swift without a storyboard. There are no outlets or actions. I am calling methods to perform the necessary actions.
private var resultSearchController: UISearchController!
private var locationSearchTable = LocationSearchTable()
func didClickSearchButton() {
locationSearchTable = LocationSearchTable()
locationSearchTable.tableView.delegate = self
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
mapSearchTable.tableHeaderView = resultSearchController?.searchBar
resultSearchController.delegate = self
resultSearchController.searchBar.delegate = self
resultSearchController.searchResultsUpdater = self
}
I am adding the searchBar to the tableView header and it does show up.
override func viewDidLoad() {
super.viewDidLoad()
layout()
}
Calling the 'layout' in the SuperView works.
private func layout() {
popupView.translatesAutoresizingMaskIntoConstraints = false
popupView.addGestureRecognizer(panRecognizer)
didClickSearchButton()
popupView.addSubview(resultSearchController.searchBar) //Tried adding this to the view, but all it did was put it in a different location.
popupView.addSubview(openTitleLabel)
popupView.addSubview(searchButton)
popupView.addSubview(mapSearchTable)
view.addSubview(popupView)
popupView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
popupView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
bottomConstraint = popupView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: popupOffset)
bottomConstraint.isActive = true
popupView.heightAnchor.constraint(equalToConstant: 467).isActive = true
searchButton.translatesAutoresizingMaskIntoConstraints = false
searchButton.leadingAnchor.constraint(equalTo: popupView.leadingAnchor, constant: 50).isActive = true
searchButton.trailingAnchor.constraint(equalTo: popupView.trailingAnchor, constant: -50).isActive = true
searchButton.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 5).isActive = true
openTitleLabel.translatesAutoresizingMaskIntoConstraints = false
openTitleLabel.leadingAnchor.constraint(equalTo: popupView.leadingAnchor, constant: 50).isActive = true
openTitleLabel.trailingAnchor.constraint(equalTo: popupView.trailingAnchor, constant: -50).isActive = true
openTitleLabel.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 5).isActive = true
openTitleLabel.bottomAnchor.constraint(equalTo: popupView.bottomAnchor, constant: 0)
mapSearchTable.translatesAutoresizingMaskIntoConstraints = false
mapSearchTable.topAnchor.constraint(equalTo: popupView.topAnchor, constant: 50).isActive = true
mapSearchTable.bottomAnchor.constraint(equalTo: popupView.bottomAnchor, constant: 10).isActive = true
mapSearchTable.leftAnchor.constraint(equalTo: popupView.leftAnchor).isActive = true
mapSearchTable.rightAnchor.constraint(equalTo: popupView.rightAnchor).isActive = true
mapSearchTable.centerXAnchor.constraint(equalTo: popupView.centerXAnchor).isActive = true
}
I can see all of the elements, but there is no focus inside the text field when clicking on the input box.
It turned out that the issue was with the alpha value that was a stored property in the UISearchBar class I created to handle the rendering of the searchBar. I had the value for the alpha set to '0' and after removing that value I was able to render the UISearchBar normally.

Search bar hides on tap

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.

iOS 9 Programmatic Constraints to a Nav Bar?

I am trying to programmatically set my constraints to the top of my UIViewController's Nav Bar, but I can't see to get it to work properly. Here is my code:
func setup() {
let containerView = UIView()
containerView.backgroundColor = .red
containerView.translatesAutoresizingMaskIntoConstraints = false
let payeeNameTextField = UITextField()
payeeNameTextField.backgroundColor = .green
payeeNameTextField.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(containerView)
view.addSubview(payeeNameTextField)
//iOS 9 Constraints
//x,y,w,h
// Constraint To Nav Bar
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
containerView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 20).isActive = true
// Constraint to Bottom Anchor
payeeNameTextField.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
payeeNameTextField.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
payeeNameTextField.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
payeeNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
I know setting it topAnchor is wrong, but I have tried to mess around with this and just cant seem to get the red background to show up.
Thanks in advance for any input!!

Resources