objective c - custom search bar using UISearchController in iOS 8 -
i have custom search bar class, , used interface builder insert search bar instance of custom class. how use search bar search bar uisearchcontroller? searchbar property read-only can't assign search bar it.
you have set initialized custom search bar returned in overriden search bar getter custom uisearchcontroller class , setup properties after searchcontroller init, way uisearchcontroller functionality retained:
public class dsearchcontroller: uisearchcontroller { private var customsearchbar = dsearchbar() override public var searchbar: uisearchbar { { return customsearchbar } } required public init?(coder adecoder: nscoder) { super.init(coder: adecoder) } override init(nibname nibnameornil: string?, bundle nibbundleornil: nsbundle?) { super.init(nibname: nibnameornil, bundle: nibbundleornil) } public init(searchresultscontroller: uiviewcontroller?, searchresultsupdater: uisearchresultsupdating?, delegate: uisearchcontrollerdelegate?, dimsbackgroundduringpresentation: bool, hidesnavigationbarduringpresentation: bool, searchbardelegate: uisearchbardelegate?, searchbarframe: cgrect?, searchbarstyle: uisearchbarstyle, searchbarplaceholder: string, searchbarfont: uifont?, searchbartextcolor: uicolor?, searchbarbartintcolor: uicolor?, // bar background searchbartintcolor: uicolor) { // cursor , bottom line super.init(searchresultscontroller: searchresultscontroller) self.searchresultsupdater = searchresultsupdater self.delegate = delegate self.dimsbackgroundduringpresentation = dimsbackgroundduringpresentation self.hidesnavigationbarduringpresentation = hidesnavigationbarduringpresentation customsearchbar.setup(searchbardelegate, frame: searchbarframe, barstyle: searchbarstyle, placeholder: searchbarplaceholder, font: searchbarfont, textcolor: searchbartextcolor, bartintcolor: searchbarbartintcolor, tintcolor: searchbartintcolor) } }
and custom searchbar:
public class dsearchbar: uisearchbar { var preferredfont: uifont? var preferredtextcolor: uicolor? init(){ super.init(frame: cgrect.zero) } func setup(delegate: uisearchbardelegate?, frame: cgrect?, barstyle: uisearchbarstyle, placeholder: string, font: uifont?, textcolor: uicolor?, bartintcolor: uicolor?, tintcolor: uicolor?) { self.delegate = delegate self.frame = frame ?? self.frame self.searchbarstyle = searchbarstyle self.placeholder = placeholder self.preferredfont = font self.preferredtextcolor = textcolor self.bartintcolor = bartintcolor ?? self.bartintcolor self.tintcolor = tintcolor ?? self.tintcolor self.bottomlinecolor = tintcolor ?? uicolor.clearcolor() sizetofit() // translucent = false // showsbookmarkbutton = false // showscancelbutton = true // setshowscancelbutton(false, animated: false) // customsearchbar.backgroundimage = uiimage() } required public init?(coder adecoder: nscoder) { super.init(coder: adecoder) } let bottomline = cashapelayer() var bottomlinecolor = uicolor.clearcolor() override public func layoutsubviews() { super.layoutsubviews() view in subviews { if let searchfield = view as? uitextfield { setsearchfieldappearance(searchfield); break } else { sview in view.subviews { if let searchfield = sview as? uitextfield { setsearchfieldappearance(searchfield); break } } } } bottomline.path = uibezierpath(rect: cgrectmake(0.0, frame.size.height - 1, frame.size.width, 1.0)).cgpath bottomline.fillcolor = bottomlinecolor.cgcolor layer.addsublayer(bottomline) } func setsearchfieldappearance(searchfield: uitextfield) { searchfield.frame = cgrectmake(5.0, 5.0, frame.size.width - 10.0, frame.size.height - 10.0) searchfield.font = preferredfont ?? searchfield.font searchfield.textcolor = preferredtextcolor ?? searchfield.textcolor //searchfield.backgroundcolor = uicolor.clearcolor() //backgroundimage = uiimage() } }
init example:
searchcontroller = dsearchcontroller(searchresultscontroller: ls, searchresultsupdater: self, delegate: self, dimsbackgroundduringpresentation: true, hidesnavigationbarduringpresentation: true, searchbardelegate: ls, searchbarframe: cgrectmake(0.0, 0.0, screen_width, 44.0), searchbarstyle: .minimal, searchbarplaceholder: nslocalizedstring("search location...", comment: ""), searchbarfont: nil, searchbartextcolor: nil, searchbarbartintcolor: uicolor.whitecolor(), searchbartintcolor: iconscolor) searchcontroller.searchbar.keyboardappearance = .dark definespresentationcontext = true tableview.tableheaderview = searchcontroller.searchbar
Comments
Post a Comment