ios – 如何向JSQMessagesViewController添加一个搜索栏

前端之家收集整理的这篇文章主要介绍了ios – 如何向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,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]|",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("")
}

解决方法

首先:UIViweController的JSQMessagesViewController子类.这意味着您可以轻松添加子视图.您在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)]",views: views))

    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[searchBar]|",views: views))

    self.view.addConstraint(NSLayoutConstraint(item: searchBar,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]];
原文链接:https://www.f2er.com/iOS/337207.html

猜你在找的iOS相关文章