1

私はUIToolbar5つのアイテムを持っています:

  1. UIBarButtonItem画像あり
  2. UIBarButtonItemフレックス幅
  3. UIBarButtonItemカスタムビュー付き ( UISearchBar)
  4. UIBarButtonItemフレックス幅
  5. UIBarButtonItem画像あり

を選択するとUISearchBar、正しいサイズを維持してアクティブになります。ただし、左右の画像が消えて、UISearchBarの全幅を与えたいと思いUIToolbarます。それ、どうやったら出来るの?

これは私が持っているものです: https://dl.dropbox.com/u/3077127/demo_1.mov

そして、これが私が望むものです: https://dl.dropbox.com/u/3077127/demo_2.mov

これは私のコードです:

#pragma mark View lifecycle

- (void)viewDidLoad {
[...]

    SearchBar *mySearchBar = [[SearchBar alloc] init];
    mySearchBar.delegate = self;
    [mySearchBar sizeToFit];
    [mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [mySearchBar setTintColor:[UIHelpers getSlColor]];

    CGRect searchBarFrame = mySearchBar.frame;
    searchBarFrame.size.width = 218;
    [mySearchBar setFrame:searchBarFrame];

    UIView *searchBarContainer = [[UIView alloc] initWithFrame:mySearchBar.frame];
    [searchBarContainer addSubview:mySearchBar];
    [searchBarContainer setTag:99];

    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];

    NSArray *items = [[NSArray alloc] initWithObjects:left, flex, search, flex, right, nil];

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolbar setItems:items];
    [toolbar setTintColor:[UIHelpers getSlColor]];

    self.tableView.tableHeaderView = toolbar;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectNull];

[...]

    [super viewDidLoad];
}

#pragma mark -
4

3 に答える 3

2

ここに投稿された同様の質問に対する私の答えはここにあります

正しいアニメーションの鍵はUIViewAnimationOptionLayoutSubviews、アニメーションのオプションとして指定することです。

于 2012-12-21T16:36:59.200 に答える
0

そのために ToolBar を使用しないでください。右と左の 2 つの ButtonItem で NavigationBar を使用し、NavigationBar の titleView に適切なデリゲートを使用して SearchBar を追加するだけです。

したがって、NavigationBar のサイズを変更する必要はありません。次のようにアニメーション化された NavigationBar の左右の ButtonItem を追加/削除するだけです。

if (self.searchBar.isActive) {
     [self.navigationItem setLeftBarButtonItem:nil animated:YES];
     [self.navigationItem setRightBarButtonItem:nil animated:YES];
}
else {
    [self.navigationItem setLeftBarButtonItem:self.leftButton animated:YES];
    [self.navigationItem setRightBarButtonItem:self.rightButton animated:YES];
}

お役に立てれば!

于 2012-12-21T16:46:21.093 に答える
0

これを試したかどうかはわかりませんが、UISearchBarDelegateメソッドを使用して、ユーザーが検索バーをクリックしたときに通知を受け取ることができるかもしれません。

例:

    #pragma mark - UISearchBarDelegate Methods

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
      // Remove the right and left buttons from the ToolBar
      [self updateToolBarWithImages:NO];
    }

    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
      // Add back the right and left buttons to the ToolBar
      [self updateToolBarWithImages:YES];
    }


    #pragma mark - Private Methods

    // Custom method used to Update the ToolBar
    - (void)updateToolBarWithImages:(BOOL)iShowImages {
      // Logic to update ToolBar based on the BOOL value in showImages
      // use "setItems:animated:" method of UIToolBar to set the ToolBar Items
      [toolBar setItem:items animated:YES];
    }
于 2012-12-17T10:19:33.377 に答える