UISearchDisplayController animate Cancel button

FUCK, SHIT, etc. 一系列脏话,日了等。为啥,因为烦的要命。主要是碰到了一个问题,我要在UIsearchBar的左边加一个按钮,这样UISearchBar的width就变小窄了。但是每次点击Cancel的时候,UISearBar又填充了整个视图的宽度。如下图:

 

解决方法:

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController*)controller
{// animate the search bar to the left ie. x=0[UIView animateWithDuration:0.25f animations:^{
     CGRect frame = controller.searchBar.frame; frame.origin.x =0; controller.searchBar.frame = frame;}
];
// remove all toolbar items[self.toolbar setItems:nil animated:YES];}
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController*)controller
{// animate search bar back to its previous position and size// in my case it was x=55, y=1// and reduce its width by the amount moved, again 55px[UIView animateWithDuration:0.25f 
                          delay:0.0f// the UIViewAnimationOptionLayoutSubviews is IMPORTANT,// otherwise you get no animation// but some kind of snap-back movement 
                        options:UIViewAnimationOptionLayoutSubviews 
                     animations:^{CGRect frame =self.toolbar.frame;
                         frame.origin.y =1;
                         frame.origin.x =55;
                         frame.size.width -=55;
                         controller.searchBar.frame = frame;} 
                     completion:^(BOOL finished){// when finished, insert any tool bar items you had[self.toolbar setItems:[NSArray arrayWithObject:self.currentLocationButton] animated:YES];}];}

 如下效果:

 

 

 PS:

self.searchDisplayController.searchBar.frame=CGRectMake(10, 144, 152, 44); 这样不会改变SearchBar的宽度,需要将searchbar放在一个view上才可以。

 

转载于:https://www.cnblogs.com/iosdev/archive/2013/01/07/2849683.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值