2012-09-06 24 views
6

有没有人试过用UISearchDisplayController和UISearchBar,它是UIToolbar中的UIBarButtonItem?iOS - 将UISearchDisplayController与UISearchBar一起使用,它是UIToolbar中的UIBarButtonItem

我将不胜感激如何成功做到这一点。

但是目前无论何时搜索控制器弹出它重绘的UISearchBar,我艰难地维持着相似的外观到UIToolbar

+0

因此..你想自定义UISearchBar吗?这是个问题吗? –

+0

我想我有两个问题: 1.如果我连接UISearchDisplayController,是否应该将UISearchBar嵌入到UIToolbar中? 2.如果UISearchDisplayController中的UISearchBar的外观可以在激活这个过程时得到修复?目前它被重新绘制和重新定位,并且不保留UIToolbar中UISearchBar的外观 – Rammeln

回答

5

通常你不会想要把一个搜索栏工具栏里面,它似乎你想做一些类似于我所做的事情。

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

首先,你必须将它设置在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

相关问题