2

上部にカスタムナビゲーションバーがある標準のiPadビューコントローラーがあります。xibファイルに、ビューの右端に配置されたUISearchBarを追加しました。検索バーの幅は320pxです。私はこのようなsearchdisplaycontrollerを初期化します:

// Search display controller
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar 
                                                                                contentsController:self];
_mySearchDisplayController.delegate = self;
_mySearchDisplayController.searchResultsDataSource = self;
_mySearchDisplayController.searchResultsDelegate = self;

問題は、検索バーを押すと、バーのサイズがビュー全体の全幅になるように変更されますが、x位置は維持されることです。これは、画面のはるか外側に伸びることを意味します。検索バーの横にスライドする「キャンセル」ボタンと関係があるのではないかと思います。検索バーを画面の左端に配置すると、画面の全幅にアニメーション化され、キャンセルボタンが表示されます。

誰かがこれに対する解決策を持っていますか?

4

1 に答える 1

2

UISearchBarメソッドでフレームをアニメーション化して、次のsearchDisplayControllerWillBeginSearchように位置を修正できます。

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    // animate the search bar to the left ie. x=0
    [UIView animateWithDuration:0.25f animations:^{
        CGRect frame = controller.searchBar.frame;
        frame.origin.x = 0;
        controller.searchBar.frame = frame;
    }];
    // remove all toolbar items if you need to
    [self.toolbar setItems:nil animated:YES];
}

検索が終了したら、もう一度アニメーション化します。

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    // animate search bar back to its previous position and size
    // in my case it was x=55, y=1
    // and reduce its width by the amount moved, again 55px
    [UIView animateWithDuration:0.25f 
                          delay:0.0f
                    // the UIViewAnimationOptionLayoutSubviews is IMPORTANT,
                    // otherwise you get no animation
                    // but some kind of snap-back movement 
                        options:UIViewAnimationOptionLayoutSubviews 
                     animations:^{
                         CGRect frame = self.toolbar.frame;
                         frame.origin.y = 1;
                         frame.origin.x = 55;
                         frame.size.width -= 55;
                         controller.searchBar.frame = frame;
                     } 
                     completion:^(BOOL finished){
                         // when finished, insert any tool bar items you had
                         [self.toolbar setItems:[NSArray arrayWithObjects: /* put your bar button items here */] animated:YES];
                     }];
}

私は同様の質問に答えました、あなたはそれをここでチェックすることができます、私もいくつかの画像を入れました。

あなたがしなければならない唯一のことは、iPad用にコードを適応させることです。

于 2012-09-19T17:46:03.090 に答える