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:
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,
- add
uisearchbardelegate
class in
viewdidload()
override func viewdidload(){ super.viewdidload() searchcontroller.searchbar.delegate = self }
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
Post a Comment