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

不确定您是否尝试过,但也许您可以使用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 回答
0

您不应该为此使用 ToolBar 。只需使用带有 2 个左右 ButtonItems 的 NavigationBar,并在 NavigationBar 的 titleView 中添加一个带有适当代表的 SearchBar。

因此,您不必调整 NavigationBar 的大小。这只是添加/删除 NavigationBar 的左右 ButtonItems 动画,如下所示:

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 回答