gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:05:25 24 4
gpt4 key购买 nike

我有一个包含 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com