2015-04-25 11 views
13

W mojej aplikacji do czatu używam JSQMessagesViewController do renderowania konwersacji. Aplikacja ma również publiczne wiadomości, które chcę przeszukać. Teraz próbuję wyświetlić je za pomocą JSQMessagesViewController. W tym celu chcę ukryć inputToolbar (który działa) i dodać pasek wyszukiwania.Jak dodać pasek wyszukiwania do JSQMessagesViewController

Jak ustawić pasek wyszukiwania jako widoczny? Kiedy patrzysz na właściwość topContentAdditionalInset, wygląda na to, że powinno być możliwe. Oto mój kod z moich prób:

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

Aktualizacja:

Poniższy kod wydaje się działać racjonalnie OK. Kwestie z nim związane są następujące: - To dziecko z kolekcjiView i dlatego będzie przewijać zawartość poza zasięgiem wzroku. Dodanie go do podglądu .superview nie działa. - przewija w dół 44 piksele, gdy fokus zostanie ustawiony.

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

Aktualizacja 2:

podstawie odpowiedzi Sergey (która działa) Jestem teraz używając następującego kodu:

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

Czy próbowali dodanie paska wyszukiwania jako nagłówka widoku kolekcji za widok? – BHendricks

Odpowiedz

10

przede wszystkim: JSQMessagesViewController podklasy UIViweController. Oznacza to, że możesz łatwo dodawać subviews. Prawie wszystko wykonałeś poprawnie w Attempt 2, ale ustawiłeś niepoprawną ramkę. Proponuję użyć układ automatycznego za to jak w kodzie poniżej:

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

Dzięki, teraz działa dokładnie tak, jak powinno. –

+0

Łamie wiązania. – Fengson

Powiązane problemy