1

私は結果を表示するためにUISearchControllerを利用するViewControllerサブクラスを構築しようとしています。ビューには、画面の最上部までアニメーション化したい入力フィールドがあり、ユーザーは結果を表示するために最大限の余裕があります。UISearchBarはプログラムの制約を無視します

検索バーのドロップインコンポーネントをラップし、ストーリーボードでトップ、リーディング、トレーリング、およびボトムの制約を同じに設定するUIViewを使用して概念証明を作成しました。次のコードは、異なる動きをアニメーション化する責任がある:

- (void)keyboardWillAppear:(NSNotification*)notification { 
    self.labelToSearchBarSpaceConstraint.active = NO; 
    self.searchBarAtTopConstraint.active = YES; 
    self.searchBarLeadingSpaceConstraint.constant = 0; 
    self.searchBarTrailingSpaceConstraint.constant = 0; 

    [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

- (void)keyboardWillDisappear:(NSNotification*)notification { 
    self.searchBarAtTopConstraint.active = NO; 
    self.labelToSearchBarSpaceConstraint.active = YES; 
    self.searchBarLeadingSpaceConstraint.constant = 20; 
    self.searchBarTrailingSpaceConstraint.constant = 20; 

    [UIView animateWithDuration: [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

が、私はその後、プログラム的UISearchControllerUISearchBar作成に切り替えてみました。次のコードは、私は、コード内の代わりに、ストーリーボードで同等の関係だと思ったものを設定しています。

self.definesPresentationContext = YES; 

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
UISearchBar* searchBar = self.searchController.searchBar; 
searchBar.translatesAutoresizingMaskIntoConstraints = NO; 
[self.searchBarView addSubview:searchBar]; 

[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0].active = YES; 

しかし、UISearchBarオブジェクトが制約に従わないこの方法で行われたとき。コンテナがアニメーション化されると、検索バーがページ外に飛びます。なぜこうなった?

マイナーアップデート:NSLayoutConstraintのドキュメントで明示的に追加するのではなく、アクティブプロパティをYESに設定することに気付きましたが、動作はまったく同じです。一致するコードを更新しています。

答えて

0

UITearchControllerのsearchBarをUITableViewヘッダービューに追加するときに同様の問題が発生しました。

ユーザーがsearchBarを選択した後にUITableViewを保持しているcontainerViewの制約をアニメートすると、containerViewに従わなくなります。

私の場合、それはデリゲートdidPresentSearchController後ように思われる:(UISearchController *) searchControllerが呼び出され、検索バーがUISearchBarContainerViewのスーパーの代わりのUITableViewのヘッダビューを持っていました。

動画を再生する前に元のヘッダービューに戻し直してください。

-(void)didPresentSearchController:(UISearchController *)searchController { 

    NSLog(@"did present search controller"); 

    self.tableView.tableHeaderView = self.searchController.searchBar; 

    ...animation... 

} 
関連する問題