2012-05-30 158 views
0

我使用UISearchBar + Interface Builder中的UISearchBar + SearchDisplayController提供的UISearchBar,搜索栏位于视图的右侧,我需要在激活时向左扩展,但是系统似乎向右扩展(在我的情况下,它超越了屏幕),我试图在启动时对它进行动画处理,但系统的动画仍然存在,并且存在一些奇怪的动画。UISearchBar:奇怪的扩展动画

有什么办法解决这个问题吗?

非常感谢!

回答

2

这里是我的回答类似的问题发布:herehere

到正确的动画,关键是要指定UIViewAnimationOptionLayoutSubviews作为动画的选项。

这里是我的全部答案,这些类似的问题:

通常你不会想要把一个搜索栏工具栏里面,但是,似乎你想要做的相似,我做了一件。

因此,这里是我是如何做到的,它可以被称为黑客,但它就像一个魅力:)

首先,你必须将它设置在Interface Builder是这样的:

enter image description here

请注意,搜索不是工具栏的子项,而是位于上方。

搜索栏应该有“清晰的颜色”背景和灵活的左,右和宽度自动调整掩码。

您在工具栏下面放置一个黑色背景的1像素标签,即。 [x = 0,y = 44,width = 320或frame width,height = 1],也是灵活的左,右和宽度自动调整掩码。这是为了在搜索显示控制器显示表格后隐藏您获得的一个可见像素视图。尝试一下没有理解我的意思。

您设置了任何工具栏项目,并确保有他们的网点,因为您将需要这些。

,现在的代码...

当你开始搜索

- (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]; 
        }]; 
} 

有了这个,我得到了一个漂亮的动画如下: )

enter image description here

enter image description here