【问题标题】:iOS - using UISearchDisplayController with UISearchBar that is UIBarButtonItem in UIToolbariOS - 将 UISearchDisplayController 与 UISearchBar 一起使用,即 UIToolbar 中的 UIBarButtonItem
【发布时间】:2012-09-06 22:37:11
【问题描述】:

有没有人尝试过使用 UISearchDisplayController 和 UISearchBar,它是 UIToolbar 中的 UIBarButtonItem?

我将不胜感激有关如何成功执行此操作的提示。

目前,每当搜索控制器弹出时,它都会重绘 UISearchBar,我正在努力保持与 UIToolbar 相似的外观

【问题讨论】:

  • so.. 你想自定义 UISearchBar?是这个问题吗?
  • 我想我有两个问题: 1. 如果我附加了一个 UISearchDisplayController,我是否应该在 UIToolbar 中嵌入 UISearchBar? 2. 如果UISearchDisplayController中的UISearchBar的外观在激活时可以修复,这是怎么做的?目前它已重新绘制和重新定位,并且不保留 UIToolbar 中 UISearchBar 的外观

标签: ios uisearchbar uitoolbar uisearchdisplaycontroller


【解决方案1】:

通常您不想将搜索栏放在工具栏内,但是,您似乎想做一些类似于我所做的事情。

这就是我的做法,它可能被称为 hack,但它就像一个魅力:)

首先你必须像这样在界面生成器中设置它:

请注意,搜索不是工具栏的子项,而是在上面。

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

您在工具栏下方放置了一个黑色背景的 1 像素标签,即。 [x=0, y=44, width=320 or 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];
                     }];
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2014-01-22
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多