3

当我从 leftBarButtonItem 调整 UISearchBar 的大小时,我试图添加一个简单的动画。出于某种原因,尽管 searchBar 的大小已修改,但调整大小不会动画。但是,如果我尝试修改 searchBar 的 alpha,它会生成动画。

这是代码

UISearchBar *baseSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:baseSearchBar];

动画方法是:

  [UIView animateWithDuration:2.0f animations:^{
    searchBar.frame = CGRectMake(0, 0, 400, 40);
    searchBar.alpha = 0;
} completion:^(BOOL finished) {  
}];

正如我所说,调整大小确实有效,但没有动画效果,如果我尝试更改 alpha,动画效果。

还有一件事......我尝试为 rightBarButtonItem 做同样的事情并且动画工作......

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(expandWithAnimation:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Test" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 40);
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

先感谢您

4

0 回答 0