如何将搜索栏添加到JSQMessagesViewController

时间:2015-04-25 07:28:38

标签: ios search chat customizing 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("")
}

1 个答案:

答案 0 :(得分:10)

首先: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]];