无法为UISearchBar的框架或边界设置动画

时间:2012-03-09 15:13:08

标签: ios animation uisearchbar

我想为UISearchBar的位置设置动画,但是当我更改UISearchBar的框架或边界属性(中心,alpha剂量动画)时,没有动画效果。
这是iOS SDK 4.2,有没有错误?我很困惑......

3 个答案:

答案 0 :(得分:9)

问题是某些内部搜索栏视图强制调整大小以忽略动画。

这对我有用 -

[UIView animateWithDuration:0.2 animations:^ {
                     [searchBar setFrame:searchBarFrame];
                     [searchBar setNeedsLayout];
                 }];

searchBarFrame - 是您需要设置的框架(我在搜索栏第一次调整大小之前保存它)

答案 1 :(得分:1)

使用UIViewAnimationOptionLayoutSubviews选项:

[UIView animateWithDuration:.25
                      delay:0
                    options:UIViewAnimationOptionLayoutSubviews
                 animations:^{
                     CGRect frame = searchBar.frame;
                     frame.size.width = newWidth;
                     searchBar.frame = frame;
                 } completion:nil
 ];

答案 2 :(得分:0)

试试这个:

[UIView animateWithDuration:0.3 animations:^{
    self.searchDisplayController.searchBar.frame = CGRectMake(0, 200, 320, 44);
}];