ios - 如何将搜索栏添加到 JSQMessagesViewController

标签 ios search chat customizing jsqmessagesviewcontroller

在我的聊天应用程序中,我使用 JSQMessagesViewController用于呈现对话。该应用程序还有我想要搜索的公共(public)消息。我现在正尝试使用 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:

根据 Sergey 的回答(有效),我现在使用以下代码:

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("")
}

最佳答案

首先:JSQMessagesViewControllerUIViweController 的子类。这意味着您可以轻松添加 subview 。您几乎在 Attempt 2 中正确完成了所有操作,但设置了错误的框架。我建议您为此使用自动布局,如下面的代码所示:

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]];

关于ios - 如何将搜索栏添加到 JSQMessagesViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862354/

相关文章:

ios - 如何以编程方式检查 NSMicrophoneUsageDescription

android - 在 Android 和 IOS 之间选择使用哪种音频文件格式?

ios - Coreplot 绘图空间范围 - iOS

ios - 在 DialogViewController 中滚动破坏了单元格点击事件

chat - 电报机器人 : secret chats possible?

Android软键盘隐藏RecyclerView

PHP:如何创建链接到我的搜索脚本的搜索表单

java - 在 Set 中查找包含前缀的条目

css - Firefox 中搜索栏上的占位符文本未对齐

communication - 您是否出于工作目的在线聊天?