2011-11-04 103 views
6

不知何故,我知道为什么默认动画失败。UISearchBar不会向上移动

我的搜索栏没有按原本的方式向上移动。

该视图是在UIView,我想知道如果这是问题。

包括IB布局

enter image description here enter image description here enter image description here

+0

请详细说明问题我不理解你的问题 –

+0

嗨维杰,当我点击搜索栏时,它应该像第一张图片一样推高。但是我的搜索栏(第二张图片)推高了导航栏,但没有移动到顶部 – Desmond

+0

应该推高什么?键盘?我仍然不明白你的问题。 – Robert

回答

11

搜索栏实际上并不在 '俯卧撑' 动画中移动。相反,当导航栏上升时,它会保持原位,从而将剩下的视图与它拉开。您应该可以通过注册代理(UISearchBarDelegate)并响应这些调用来在代码中手动移动搜索栏。

#pragma mark - UISearchBarDelegate Methods 
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { 
    //move the search bar up to the correct location eg 
    [UIView animateWithDuration:.4 
       animations:^{ 
        searchBar.frame = CGRectMake(searchBar.frame.origin.x, 
                0, 
                searchBar.frame.size.width, 
                searchBar.frame.size.height); 
       } 
       completion:^(BOOL finished){ 
        //whatever else you may need to do 
       }]; 
} 

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { 
    //move the search bar down to the correct location eg 
    [UIView animateWithDuration:.4 
       animations:^{ 
        searchBar.frame = CGRectMake(searchBar.frame.origin.x, 
                /*SearchBar original Y*/, 
                searchBar.frame.size.width, 
                searchBar.frame.size.height); 
       } 
       completion:^(BOOL finished){ 
        //whatever else you may need to do 
       }]; 
} 
+0

嗨安德鲁,谢谢你是我想做的事情:) 会在7小时内奖励你赏金时间,现在不能奖励你:( – Desmond

+0

它会好的你帮我解决这个问题。 http://stackoverflow.com/questions/8027984/how-to-have-a-overlay-on-top-of-a-select-uitabbar-item and http ://堆栈溢出。com/questions/8028411 /哪些事件得到称为当你点击在uisearchbar致电首先响应 – Desmond

1

在您的问题的第一张图片中,您显示的是正在使用的UISearchDisplayController。为了获得精确的动画,您需要使用UISearchDisplayController并将其修改为UISearchBar,使其看起来像您制作的自定义动画。您可以在Interface Builder中拖动UISearchDisplayController,然后使用代码更改其searchBar属性。

self.searchDisplayController.searchbar将被修改,如果你已经在Interface Builder中正确连接了所有东西。

2

实现一个UISearchBarUISearchDisplayController下苹果的指导方针,最好的办法是看所谓TableSearch的例子中,描述说:

演示如何搜索使用 的UISearchBar和UISearchDisplayController一个UITableView中的内容,在 中进行有效筛选,然后输出该表的内容。如果iPhone/iPod Touch 应用程序具有大量的表格数据,则此示例说明如果 将内容过滤到可管理的数量(如果内存使用情况值得关注)或 您只希望用户滚动浏览较少的表格内容。

我希望这可以帮助你。

+0

嗨猫王感谢您的答案,我一直在使用搜索显示控制器。 – Desmond

+0

这是我五小时前回答的问题 –