swift - How to add another textfield to UISearchController when focus is on Search Bar? -


i'm trying add textfield 'location' input uisearchcontroller when user focuses on search bar, below search bar on navigation bar.

example of have , i'd go:

enter image description here

i've tried this, doesn't work:

var searchcontroller: uisearchcontroller!  func searchbartextdidbeginediting(searchbar: uisearchbar) {     var mytextfield: uitextfield = uitextfield(frame: cgrect(x: 0, y: 0, width: 200.00, height: 40.00))     mytextfield.backgroundcolor = uicolor.purplecolor()     mytextfield.text = "location"     mytextfield.borderstyle = uitextborderstyle.none     searchcontroller.view.addsubview(mytextfield) } 

any great! thanks.

don't know whether found answer or not. let me suggest solution.

nothing wrong in code. works fine. reason why doesn't show , when click in search bar func searchbartextdidbeginediting(searchbar: uisearchbar) has no effect on because might forget set delegate search bar.

i tried code , after setting delegate works fine. hides behind navbar y position 0. solution is,

  1. add uisearchbardelegate class
  2. in viewdidload()

      override func viewdidload(){     super.viewdidload()     searchcontroller.searchbar.delegate = self     } 
  3. in searchbartextdidbeginediting

       func searchbartextdidbeginediting(searchbar: uisearchbar) {                 var mytextfield: uitextfield = uitextfield(frame: cgrect(x: 0, y: searchcontroller.searchbar.frame.height + 20, width: view.frame.size.width, height: 40.00))     mytextfield.backgroundcolor = uicolor.purplecolor()     mytextfield.text = "location"     mytextfield.borderstyle = uitextborderstyle.none     searchcontroller.view.addsubview(mytextfield) } 

hope helps.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -