ios - How do I put UISearchController Searchbar onto navigation bar with code -
if embed viewcontroller navigation bar, navigationitem.titleview.resultsearchcontroller?.searchbar
put search bar navigation bar. however, i've created uisearchcontroller , uinavigationbar code. time, navbar showing up, searchbar isn't.
resultsearchcontroller = uisearchcontroller(searchresultscontroller: locationsearchtable) resultsearchcontroller?.searchresultsupdater = locationsearchtable let searchbar = resultsearchcontroller!.searchbar searchbar.sizetofit() searchbar.delegate = self let navbar: uinavigationbar = uinavigationbar(frame: cgrect(x: 0, y:0, width: 375, height: 64)) self.view.addsubview(navbar) //navbar.topitem = resultsearchcontroller?.searchbar self.navigationitem.titleview = resultsearchcontroller?.searchbar
navbar.topitem = resultsearchcontroller?.searchbar
doesn't work because topitem
string value , resultsearchcontroller?.searchbar
uiview
type. how can achieve same effect?
create uinavigationitem
instance , add created navigation bar. add search controller search bar uinavigationitem titleview.
class searchviewcontroller: uiviewcontroller, uisearchcontrollerdelegate, uisearchresultsupdating, uisearchbardelegate { var searchcontroller: uisearchcontroller! override func viewdidload() { super.viewdidload() self.addnavigationbar() } func addnavigationbar() { let navbar = uinavigationbar(frame: cgrect(x: 0, y: 0, width: self.view.frame.size.width, height: 60)) self.view.addsubview(navbar) let navigationitem = uinavigationitem(title: "") self.searchcontroller = searchcontrollerwith(searchresultscontroller: nil) navigationitem.titleview = self.searchcontroller.searchbar navbar.setitems([navigationitem], animated: false) self.definespresentationcontext = true } func searchcontrollerwith(searchresultscontroller: uiviewcontroller?) -> uisearchcontroller { let searchcontroller = uisearchcontroller(searchresultscontroller: searchresultscontroller) searchcontroller.delegate = self searchcontroller.searchresultsupdater = self searchcontroller.searchbar.delegate = self searchcontroller.hidesnavigationbarduringpresentation = false searchcontroller.dimsbackgroundduringpresentation = true return searchcontroller }
Comments
Post a Comment