【问题标题】:Resizing UIToolbar when UISearchBar becomes active当 UISearchBar 变为活动状态时调整 UIToolbar 的大小
【发布时间】:2012-12-13 19:31:58
【问题描述】:

我有一个UIToolbar,里面有 5 个项目:

  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 -

【问题讨论】:

  • 嘿,在这里查看我的 SO 答案link,这与您尝试做的类似。
  • 这似乎有效。但是,我不使用界面生成器,而是通过代码完成所有操作......这也应该可以。将您的回复添加为答案,我会批准。
  • 很高兴能提供帮助 :) 将其作为答案发布。

标签: objective-c ios uiview uisearchbar uitoolbar


【解决方案1】:

这是我对here发布的类似问题的回答

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我曾尝试更新工具栏,但如果我没记错的话它并不顺利。由于工具存在一个包含项目的数组,因此更新项目会导致突然更新(不是动画)或根本不发生。
    • 你试过UIToolBar的- (void)setItems:(NSArray *)items animated:(BOOL)animated;方法来设置ToolBar Items吗?相应地更新了答案。希望这会有所帮助。
    • 我想我做到了,但我目前不确定。我今晚一回到家就试试看是否有效。
    • 不幸的是,它没有给我想要的结果。
    【解决方案3】:

    您不应该为此使用工具栏。 只需使用带有 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];
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-22
      • 2014-06-01
      • 2017-03-09
      相关资源
      最近更新 更多