uisearchbar - Set inputview for Searchbar - Swift -
class testviewcontroller: uiviewcontroller, uisearchbardelegate { @iboutlet weak var mysearchbar: uisearchbar! var myowninputview = uiview(frame: cgrectmake(100,100,50,50)) overide func viewdidload() { mysearchbar.delegate = self mysearchbar.inputview = myowninputview // errors - "cannot assign result of expression } }
i'm trying assign custom input view search bar, based on apple documentation (-sorry not find link anymore), uisearchbar seems read-only value. looking @ this post, appears uisearchbar has multiple subviews , need uitextfield part in order change inputview. however, i'm not sufficiently familiar obj-c , attempts convert code swift have not been successful.
found out textfield
subview of subview of uisearchbar
.
code textfield in swift:
var fakeview: uiview = uiview(frame: cgrectmake(100, 100, 50, 50)) @iboutlet weak var searchbar: uisearchbar! override func viewdidload() { fakeview.backgroundcolor = uicolor.redcolor() var c = 0 v in (self.searchbar.subviews[0]).subviews { c++ println("\(c).\(v)") //you should see 2 views - uisearchbarbackground , uisearcbartextfield if let tf = v as? uitextfield { //do stuff tf here. //in case, want is: tf.inputview = fakeview break } } }}
result of above code no keyboard show when searchbar text field touched, red rectangle.
note - credit goes matt neuburg's "programming ios8: dive deep views, viewcontrollers, , frameworks (isbn: 978-1491908730). chapter 8 precise.
Comments
Post a Comment