13

在我的聊天应用程序中,我使用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("")
}
4

1 回答 1

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]];
于 2015-05-07T11:06:19.233 回答