2

I have looked everywhere and there doesn't seem to be a clear example. I am trying to achieve this, where the textField will act as a searchBar then the user would be able to add that item: enter image description here

Most example seem to use UI SearchBar and a tableView where it displays the filtered results. But there is no real example that uses just a textField.

My other option is to try and just get an autocompletion where it checks if the character that is inputted matches an array of data, something like this:

Are there any clearer examples or tutorial I can follow? I'm fairly new to programming so I would prefer it be in Swift please.

Other similar reference:

http://www.iostutorialjunction.com/2017/04/integrate-search-functionality-in-ios-using-swift3.html

How to make UITextField behave like a UISearchBar in Swift?

https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

http://www.matthewhsingleton.com/blog/2016/5/26/predictive-text-table-view-swift-version-of-ray-wenderlich

https://www.youtube.com/watch?v=YZ_S2GHFwW4

https://www.youtube.com/watch?v=XtiamBbL5QU&t=975s

Getting autocomplete to work in swift

https://github.com/EddyBorja/MLPAutoCompleteTextField

Community
  • 1
  • 1
Chace
  • 561
  • 10
  • 28

2 Answers2

3

There are many ways to accomplish this task. You can place a textField as well as a UILabel exactly behind it. And When Every you type a word just change the text of that label. Set the Label's textColor to lightGrey color and you will be good to go. To set a method that invokes every time user type something use below code.

//Below line will set a method to textFields value change event.
searchTextField.addTarget(self, action: #selector(self.textIsChanging(textField:)), for: .editingChanged)



func textIsChanging(textField: UITextField) {

}
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • Thanks for this response. What do you mean by "place a textField as well as a UILabel exactly behind it", behind what. Also in the function textIsChanging(textField: UITextField) is this where would check if the inputted data is equal to some string in an array? – Chace May 18 '17 at 20:58
  • Yes this is the method where you will check if the entered text is equal to some string in array or not. And Go to storyboard place a Label behind UITextField. Give textField a clearColor and on searching a match string just update the uilabel's text not the textField's text. This will give you the effect that you want in you above image – Syed Qamar Abbas May 19 '17 at 07:29
0

you can use an delegate method textFieldDidChangeSelection(_ textField: UITextField), works for me

Tmaximo
  • 33
  • 9