objective-c - 当 UISearchBar 激活时调整 UIToolbar 的大小

标签 objective-c ios uiview uisearchbar uitoolbar

我有一个包含 5 个项目的 UIToolbar:

  1. UIBarButtonItem 带图片
  2. UIBarButtonItem 弹性宽度
  3. UIBarButtonItem 带有自定义 View (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 -

最佳答案

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

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

关于objective-c - 当 UISearchBar 激活时调整 UIToolbar 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867059/

相关文章:

iphone - 在某些部分隐藏 UITableViewStyleGrouped 分隔符

ios - Objective C - 是否可以从另一个相同类型的类别调用一个类别的方法?

objective-c - 为什么我不能复制 AVCapture Session?

ios - CATextLayer 文本字体随着文本大小的增加而消失

ios - performSegueWithIdentifier 在 iOS 9.x 中导致 NSInternalInconsistencyException 而不是在 8.x 中

iphone - 由于某些字符,NSData 到 NSString 的转换失败

ios - SwiftUI 将 navigationBarItems 按钮与大 navigationBarTitle 对齐,如 "Messages"

ios - 如何制作未知页码的UIPageViewController?

ios - 如何模仿 map 应用程序的底部工作表?

iOS 重现一个 View n 次