uisearchbar - Swift 4 - iOS 11 Search bar scope won't appear as it should -
i have app works nice , without problems on ios 10 ios 11 , xcode beta 5, have strange problem search bar scope scope bar seems cut bottom. (it same versions of xcode beta , ios 11) really glad if can put me in right direction. thanks
above can see see when press hamburger menu button. no problem here.. searchbar has scopebar shows when clicked on.i did not set show programatically.but guess default behaviour when scope button titles set.
the problem:
when click on searchbar enter information see screen above. on ios 10 had no problems. now, ios 11, whatever cannot make work works on ios 10. search scope bar shows cut bottom.
this how shows in ios 10 , want in ios 11.
storyboard view of screen
i pasting relevant code here.
class slidemenuviewcontroller: uiviewcontroller,uitableviewdelegate,uitableviewdatasource,uisearchcontrollerdelegate,uigesturerecognizerdelegate{ let searchcontroller = uisearchcontroller(searchresultscontroller: nil) @iboutlet var sview: uiview! @iboutlet var tableview: uitableview! override func viewwillappear(_ animated: bool) { //some non relevant code before if sview.subviews .contains(searchcontroller.searchbar) { print("already contains") } else { sview.addsubview(searchcontroller.searchbar) print(searchcontroller.searchbar.showsscopebar) searchcontroller.searchbar.sizetofit() searchcontroller.searchbar.frame.size.width = view.frame.size.width searchcontroller.searchbar.bartintcolor = searchbartintcolor searchcontroller.searchbar.tintcolor = searchtintcolor } } override func viewdidload() { //some other code searchcontroller.searchbar.delegate = self searchcontroller.searchbar.scopebuttontitles = [nslocalizedstring("İsimlerde", comment: ""), nslocalizedstring("açıklamalarda", comment: "")] searchcontroller.searchbar.returnkeytype = uireturnkeytype.done searchcontroller.searchresultsupdater = self searchcontroller.dimsbackgroundduringpresentation = false definespresentationcontext = true //some other code } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { if searchcontroller.isactive && searchcontroller.searchbar.text != "" { switch scpglobal { case nslocalizedstring("İsimlerde", comment: ""): return filteredisimler.count case nslocalizedstring("açıklamalarda", comment: ""): return filteredaciklamalar.count default: print("tableview default") } } return isimlerarray.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { //some code search here nothing result in behaviour } func searchbarcancelbuttonclicked(_ searchbar: uisearchbar) { print("cancel") searchcontroller.searchbar.text = "" } func updatesearchresults(for searchcontroller: uisearchcontroller) { let searchbar = searchcontroller.searchbar let scope = searchbar.scopebuttontitles![searchbar.selectedscopebuttonindex] filtercontentforsearchtext(searchtext: searchcontroller.searchbar.text!, scope: scope) tableview.reloaddata() } } extension slidemenuviewcontroller:uisearchbardelegate { func searchbar(_ searchbar: uisearchbar, selectedscopebuttonindexdidchange selectedscope: int) { filtercontentforsearchtext(searchtext: searchcontroller.searchbar.text!, scope: searchcontroller.searchbar.scopebuttontitles![selectedscope]) } }
edit: happens if add constraints search bar. first seems good.
then when click on search bar happens..search bar moves out of screen. see arrow.
but if close sliding menu , re-open then
everything works ok until click cancel button. after have again see search bar working.
code constraints
searchcontroller.searchbar.translatesautoresizingmaskintoconstraints = false sview.addconstraint(nslayoutconstraint(item: searchcontroller.searchbar, attribute: .top, relatedby: .equal, toitem: sview, attribute: .top, multiplier: 1, constant: 0)) sview.addconstraint(nslayoutconstraint(item: searchcontroller.searchbar, attribute: .bottom, relatedby: .equal, toitem: sview, attribute:.bottom, multiplier: 1, constant: 0)) sview.addconstraint(nslayoutconstraint(item: searchcontroller.searchbar, attribute: .leading, relatedby: .equal, toitem: sview, attribute: .leading,multiplier: 1, constant: 0)) sview.addconstraint(nslayoutconstraint(item: searchcontroller.searchbar, attribute: .trailing, relatedby: .equal, toitem: sview, attribute: .trailing, multiplier: 1, constant: 0))
i able resolve issue replacing searchcontroller
custom searchbar
.
let searchbar = uisearchbar(frame:cgrect(x: 0, y: 0, width: 266, height: 100))
be careful though, not forget use
searchbar.sizetofit
inside searchbarshouldbeginediting
, searchbarshouldendediting
or else might have strange ui problems if using
searchbar.showsscopebar = true
so, if having similar problem, rid of searchcontroller
, implement searchbar
, delegate methods. not give same scopebar animation @ least works. downside method is, if have searchbar.istranslucent = true
might see ghost of scopebar inside searchbar. make sure that
searchbar.istranslucent = false
if comes better way solve problem, ears...
sample delegate setup
func searchbarshouldbeginediting(_ searchbar: uisearchbar) -> bool { searchbar.showsscopebar = true searchbar.sizetofit() searchbar.setshowscancelbutton(true, animated: true) return true } func searchbarshouldendediting(_ searchbar: uisearchbar) -> bool { searchbar.showsscopebar = false searchbar.sizetofit() searchbar.setshowscancelbutton(false, animated: true) return true }
Comments
Post a Comment