Can't animate the frame or bounds of a UISearchBar

后端 未结 3 1684
挽巷
挽巷 2020-12-21 01:11

I want to animate the position of a UISearchBar, but there\'s no animate effect when I change the frame or bounds property of UISearchBar(center, alpha dose animate).

相关标签:
3条回答
  • 2020-12-21 01:51

    Use the UIViewAnimationOptionLayoutSubviews option:

    [UIView animateWithDuration:.25
                          delay:0
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         CGRect frame = searchBar.frame;
                         frame.size.width = newWidth;
                         searchBar.frame = frame;
                     } completion:nil
     ];
    
    0 讨论(0)
  • 2020-12-21 02:09

    The problem is that some inner search bar views forcing the resize to ignore the animation.

    This worked for me -

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

    Where searchBarFrame - is a frame you need to set (I save it before search bar resized first time)

    0 讨论(0)
  • 2020-12-21 02:13

    Try this:

    [UIView animateWithDuration:0.3 animations:^{
        self.searchDisplayController.searchBar.frame = CGRectMake(0, 200, 320, 44);
    }];
    
    0 讨论(0)
提交回复
热议问题