首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 7 UISearchDisplayController动画故障

iOS 7 UISearchDisplayController动画故障
EN

Stack Overflow用户
提问于 2014-04-26 10:10:04
回答 2查看 1.4K关注 0票数 8

我正在尝试通过UISearchDisplayController将搜索添加到我的UITableViewController中,但是当搜索开始和结束时,我看到一个非常奇怪的动画故障。

在iPhone上,将动画放入导航栏可以很好地工作。但是,当结束搜索时,导航栏会滞后于搜索栏,同时向下显示动画。这会导致在导航栏和搜索栏之间显示一条白色条带。在iPad上,动画是完全混乱的。

iPhone video

iPad video

正如上面的视频所显示的,股票应用程序不会受到这些动画故障的影响。有没有人知道是什么导致了这些问题?

我在“主控”视图控制器中创建UISearchDisplayController,其中包含以下内容。

代码语言:javascript
复制
- (void)viewDidLoad
{
   [super viewDidLoad];

   UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
   searchBar.delegate = self;
   self.tableView.tableHeaderView = searchBar;

   self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
   self.searchController.delegate = self;
   self.searchController.searchResultsDataSource = self;
   self.searchController.searchResultsDelegate = self;
}

我也尝试过使用故事板来做这件事,但同样的动画故障也发生了。

EN

回答 2

Stack Overflow用户

发布于 2014-08-01 19:54:44

非透明导航栏解决方案(代码少,通用性差)

您看到的白色毛刺是tableview的背景颜色,通过导航栏和搜索栏之间的间隙可见。这个差距肯定是苹果开发者的疏忽造成的。

所以解决方案看起来像这样:

代码语言:javascript
复制
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    UIView *topTableViewBG = [[UIView alloc] initWithFrame:CGRectMake(0, -64, CGRectGetWidth(self.tableView.bounds), 64)];
    topTableViewBG.backgroundColor = self.navigationController.navigationBar.backgroundColor;
    topTableViewBG.tag = 1234567;
    [self.tableView insertSubview:topTableViewBG belowSubview:self.tableView.tableHeaderView];
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{   
    [[self.tableView viewWithTag:1234567] removeFromSuperview];
}

这里我在搜索栏的正下方添加了一个自定义视图。它发生在过渡到普通VC之前。视图位于特定点,因此它覆盖了导航栏和搜索栏之间的间隙。转换完成后,我删除了自定义视图。

更新:通用解决方案(代码更多,通用性更强)

只有当存在非半透明导航栏时,上面的解决方案才是好的。对于半透明导航栏,有一个挑战,以找到正确的颜色为我们的‘间隙-塞子’视图。但是一旦我们可以自由地改变搜索栏的颜色,我们就可以使用搜索栏的颜色来填补空缺。

让我们对代码进行一些更改

首先,我们需要非半透明的搜索栏,所以设置背景图片到搜索栏:

代码语言:javascript
复制
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setBackgroundImage:[UIImage pixelImageWithColor:SEARCHBAR_GRAY_COLOR] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;

下面是上面使用的UIImage类别方法:

代码语言:javascript
复制
+ (UIImage *)pixelImageWithColor:(UIColor *)color {
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB ();
    CGContextRef ctx = CGBitmapContextCreate (NULL, 1, 1, 8, 0, cs, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
    CGColorSpaceRelease (cs);
    
    CGContextSetFillColorWithColor (ctx, color.CGColor);
    CGContextFillRect (ctx, CGRectMake (0.0f, 0.0f, 1.0f, 1.0f));
    CGImageRef cgImage = CGBitmapContextCreateImage (ctx);
    CGContextRelease (ctx);
    
    UIImage *result = [UIImage imageWithCGImage:cgImage];
    CGImageRelease (cgImage);
    return result;
}

然后更改我们的searchDisplayControllerWillEndSearch方法:

代码语言:javascript
复制
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
    UIView *topTableViewBG = [[UIView alloc] initWithFrame:CGRectMake(0, -64, CGRectGetWidth(self.tableView.bounds), 64)];
    topTableViewBG.backgroundColor = SEARCHBAR_GRAY_COLOR;
    topTableViewBG.tag = 1234567;
    [self.tableView insertSubview:topTableViewBG belowSubview:self.tableView.tableHeaderView];
}

最后,searchDisplayControllerDidEndSearch方法保持不变:

代码语言:javascript
复制
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{   
    [[self.tableView viewWithTag:1234567] removeFromSuperview];
}

老实说,这个解决方案更通用,而且看起来比我在答案的第一部分中描述的要漂亮得多。

票数 1
EN

Stack Overflow用户

发布于 2015-02-20 08:14:25

我找到了最适合我的解决方案,它看起来和我需要的一模一样,我使用的是swift,但是如果你需要的话,你可以很容易地将它翻译成objective-c

代码语言:javascript
复制
self.edgesForExtendedLayout = .None

let barBacgroundView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
barBacgroundView.backgroundColor = self.tableView.backgroundColor
UIApplication.sharedApplication().delegate!.window!!.insertSubview(barBacgroundView, atIndex: 0)

viewDidLoad()中插入这段代码,就可以了。希望这能有所帮助:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23305809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档