ios - How to add a searchbar to JSQMessagesViewController -
in chat application using jsqmessagesviewcontroller rendering conversation. application has public messages want search in. trying display them using jsqmessagesviewcontroller. want hide inputtoolbar (that works) , add search bar.
how make searchbar visible? when @ property topcontentadditionalinset looks should possible. here code attempts:
override func viewdidload() { super.viewdidload() self.inputtoolbar.removefromsuperview() self.searchbar.removefromsuperview() self.topcontentadditionalinset = 44 self.searchbar.frame = cgrect(x: 0, y: 25, width: 320, height: 44) // attempt 1 // self.collectionview.addsubview(self.searchbar) // attempt 2 // self.view.addsubview(self.searchbar) // attempt 3 // self.navigationcontroller?.navigationbar.addsubview(self.searchbar) // attempt 4 // self.inputtoolbar.addsubview(self.searchbar) // attempt 5 self.collectionview.superview!.addsubview(self.searchbar) }
update:
the following code seems work reasonably ok. issues are: - it's child of collectionview , therefore scroll content out of sight. adding .superview not work. - scrolls down 44 pixels when searchbar gets focus.
var keepref:jsqmessagesinputtoolbar! var searchbar:uisearchbar! override func viewdidappear(animated: bool) { super.viewdidappear(animated) if self.inputtoolbar.superview != nil { keepref = self.inputtoolbar self.inputtoolbar.removefromsuperview() } self.topcontentadditionalinset = 44 if searchbar == nil { searchbar = uisearchbar(frame: cgrect(x: 0, y: -44, width: 320, height: 44)) searchbar.delegate = self self.collectionview.scrollstotop = true self.collectionview.addsubview(searchbar) } self.filtercontentforsearchtext("") }
update 2:
based on sergey's answer (which works) using following code:
var keepref:jsqmessagesinputtoolbar! var searchbar:uisearchbar! override func viewdidappear(animated: bool) { super.viewdidappear(animated) self.inputtoolbar.hidden = true self.topcontentadditionalinset = 44 self.collectionview.scrollstotop = true if searchbar == nil { searchbar = uisearchbar() searchbar.settranslatesautoresizingmaskintoconstraints(false) searchbar.delegate = self self.view.addsubview(searchbar) let views = ["searchbar" : self.searchbar]; searchbar.addconstraints(nslayoutconstraint.constraintswithvisualformat("[searchbar(44)]", options: nslayoutformatoptions(0), metrics: nil, views: views)) self.view.addconstraints(nslayoutconstraint.constraintswithvisualformat("|[searchbar]|", options: nslayoutformatoptions(0), metrics: nil, views: views)) self.view.addconstraint(nslayoutconstraint(item: searchbar, attribute: .top, relatedby: .equal, toitem: self.toplayoutguide, attribute: .bottom, multiplier: 1.0, constant: 0.0)) } self.filtercontentforsearchtext("") }
first of all: jsqmessagesviewcontroller
subclass of uiviwecontroller
. means can add subviews. done correctly in attempt 2
set incorrect frame. suggest use auto layout in code below:
swift
var searchbar: uisearchbar!; override func viewdidload() { super.viewdidload() searchbar = uisearchbar() searchbar.settranslatesautoresizingmaskintoconstraints(false) self.view.addsubview(searchbar) let views = ["searchbar" : self.searchbar]; searchbar.addconstraints(nslayoutconstraint.constraintswithvisualformat("[searchbar(44)]", options: nslayoutformatoptions(0), metrics: nil, views: views)) self.view.addconstraints(nslayoutconstraint.constraintswithvisualformat("|[searchbar]|", options: nslayoutformatoptions(0), metrics: nil, views: views)) self.view.addconstraint(nslayoutconstraint(item: searchbar, attribute: .top, relatedby: .equal, toitem: self.toplayoutguide, attribute: .bottom, multiplier: 1.0, constant: 0.0)) }
objective c
@property (nonatomic, strong) uisearchbar *searchbar; ... // viewdidload self.searchbar = [uisearchbar new]; self.searchbar.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:self.searchbar]; nsdictionary *views = @{@"searchbar" : self.searchbar}; [self.searchbar addconstraints:[nslayoutconstraint constraintswithvisualformat:@"[searchbar(44)]" options:0 metrics:nil views:views]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"|[searchbar]|" options:0 metrics:nil views:views]]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.searchbar attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.toplayoutguide attribute:nslayoutattributebottom multiplier:1.0 constant:0.0]];
Comments
Post a Comment