动画UISearchBar框架的宽度

Joe*_*oey 11 uisearchbar ios

是否可以为UISearchBar的帧宽设置动画?我发现当我应用uiview动画来扩大搜索栏的界限时,它会立即弹出到最终结果,好像该对象在内部控制它的动画效果并且不允许我平滑地应用我自己的动画.

如果我为位置设置动画,它会平滑移动,但我怀疑文本输入根据取消按钮的存在进行调整这一事实可能意味着我们没有公共访问权限通过UIView动画设置宽度动画.下面的示例代码段将条形图从x = 0滑动到100,但将宽度弹出为600像素宽.

CGRect searchBarFrame = self.searchViewController.searchBar.frame;
searchBarFrame.origin.x = 100;
searchBarFrame.size.width = 600;
[UIView animateWithDuration:1.0 
                      delay:0.0 
                    options:0 
                 animations:^{
                     self.searchViewController.searchBar.frame = searchBarFrame;
                 }
                 completion:^(BOOL completion){
                 }];
Run Code Online (Sandbox Code Playgroud)

小智 28

由于内部视图强制调整大小以忽略动画,因此UISearchBar存在"问题".但是,这可以通过使用 - layoutSubviews来克服.我在下面的项目中包含了扩展和合同代码

[UIView animateWithDuration:.3
                 animations:^ {
                     CGRect newBounds = locationSearch.frame;
                     newBounds.size.width += 215; //newBounds.size.width -= 215; to contract
                     locationSearch.frame = newBounds;
                     [locationSearch layoutSubviews];
                 }];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


aks*_*gde 15

仅供参考,您可以使用UIViewAnimationOption而不是layoutsubviews显式调用,因此代码看起来像这样.

[UIView animateWithDuration:0.5
                              delay:0
                            options:UIViewAnimationOptionLayoutSubviews
                         animations:^{
                             //Set the frame you want to the search bar
                         }
                         completion:^(BOOL finished) {

                         }];
Run Code Online (Sandbox Code Playgroud)