动画UISearchBar框架的宽度 [英] Animating the width of UISearchBar frame

查看:108
本文介绍了动画UISearchBar框架的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to animate the frame width of a UISearchBar? I find when I apply uiview animations to widen the bounds of a search bar it pops immediately to the final result as if the object internally is assuming control of how it animates and not allowing me to apply my own animations to it smoothly.

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

If I animate the position it moves smoothly, but I suspect the fact that the text input adjusts according to the presence of the cancel button might mean we don't have public access to animate the width through UIView animation. The sample snippet below slides the bar from x = 0 to 100 but pops the width to 600 pixels wide.

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){
                 }];


推荐答案

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

there is an "issue" with UISearchBar due to the inner views forcing the resize to ignore the animation. However, this can be overcome by the use of - layoutSubviews. I have included the expand and contract code in my project below

[UIView animateWithDuration:.3
                 animations:^ {
                     CGRect newBounds = locationSearch.frame;
                     newBounds.size.width += 215; //newBounds.size.width -= 215; to contract
                     locationSearch.frame = newBounds;
                     [locationSearch layoutSubviews];
                 }];

希望这会有所帮助。

这篇关于动画UISearchBar框架的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆