[英]How to add a searchbar to JSQMessagesViewController
在我的聊天應用程序中,我使用JSQMessagesViewController來呈現對話。 該應用程序還有我要搜索的公共消息。我現在嘗試使用JSQMessagesViewController顯示它們。 為此我想隱藏inputToolbar(可以工作)並添加一個搜索欄。
如何使搜索欄可見? 當您查看屬性topContentAdditionalInset時,它看起來應該是可能的。 這是我嘗試的代碼:
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)
}
更新:
以下代碼似乎可以正常工作。 它的問題是: - 它是collectionView的一個子項,因此將滾動顯示內容。 將其添加到.superview不起作用。 - 當搜索欄獲得焦點時,它向下滾動44個像素。
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("")
}
更新2:
基於謝爾蓋的答案(有效)我現在使用以下代碼:
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("")
}
首先: JSQMessagesViewController
的子類UIViweController
。 這意味着您可以輕松添加子視圖。 您幾乎在Attempt 2
完成了所有操作,但是您設置了錯誤的幀。 我建議您使用自動布局,如下面的代碼所示:
迅速
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))
}
目標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]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.