在UISearchBar变为活动状态时调整UIToolbar的大小

时间:2012-12-13 19:31:59

标签: objective-c ios uiview uisearchbar uitoolbar

我有一个UIToolbar有5个项目:

  1. UIBarButtonItem with image
  2. UIBarButtonItem弹性宽度
  3. UIBarButtonItem自定义视图(UISearchBar
  4. UIBarButtonItem弹性宽度
  5. UIBarButtonItem with image
  6. 当我选择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 -
    

3 个答案:

答案 0 :(得分:2)

以下是我对here

发布的类似问题的回答

正确动画的关键是指定UIViewAnimationOptionLayoutSubviews作为动画的选项。

答案 1 :(得分: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];
    }

答案 2 :(得分:0)

您不应该使用ToolBar。 只需使用带有2个左右ButtonItem的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];
}

希望这有帮助!