gpt4 book ai didi

iphone - iOS 7 UISearchDisplayController 动画故障

转载 作者:太空狗 更新时间:2023-10-30 03:33:44 25 4
gpt4 key购买 nike

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

在 iPhone 上,导航栏中的动画效果很好。但是,当结束搜索时,导航栏在向下动画时滞后于搜索栏。这会导致在导航栏和搜索栏之间显示一条白色 strip 。在 iPad 上,动画完全乱七八糟。

iPhone video

iPad video

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

我正在使用“主” View Controller 中的以下内容创建 UISearchDisplayController

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

我也尝试使用 Storyboards 来做这个,但同样的动画故障发生了。

最佳答案

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

您看到的白色故障是表格 View 的背景色,可以通过导航栏和搜索栏之间的间隙看到。差距绝对是苹果开发者的疏忽。

所以解决方案是这样的:

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

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

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

上面的解决方案只有在有非半透明的导航栏时才有效。对于半透明的导航栏,为我们的“gap-stopper” View 找到正确的颜色是一个挑战。但是一旦我们可以很自由地更改搜索栏颜色,我们就可以使用搜索栏颜色作为间隙塞。

让我们对代码做一些修改

首先,我们需要非半透明的搜索栏,因此将背景图片设置为搜索栏:

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 类别方法:

+ (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 方法:

- (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 方法保持不变:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
[[self.tableView viewWithTag:1234567] removeFromSuperview];
}

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

关于iphone - iOS 7 UISearchDisplayController 动画故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com