UISearchBar到UISearchDisplayController的转换有20像素偏差。

6
我正在UITableView顶部实现一个UISearchBar。
在我的ViewDidLoad中,我设置了self.edgesForExtendedLayout = UIRectEdgeNone
以下是我添加UISearchBar的方法。我只是将UISearchBar添加到UITableView的标头并调整内容偏移量:
// Table View
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, self.screenHeight)];

self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// Customize table appearance
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
self.tableView.backgroundColor = [UIColor tableBackgroundColor];

// Define the table's datasource
self.tableView.dataSource = self;
self.tableView.delegate = self;

[self.view addSubview:self.tableView];


// Add a search bar to the header
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.screenWidth, 44)];
self.searchBar.tintColor = [UIColor primaryBrandedColor];
self.searchBar.placeholder = NSLocalizedString(@"Search", "Search placeholder");
self.searchBar.backgroundImage = [UIImage imageNamed:@"searchBarBackground"];
[self.searchBar setBackgroundImage:[UIImage imageNamed:@"searchBarBackground"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
self.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.delegate = self;

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Whitney-Light" size:15]];

//Setup search display controller
self.searchController = [[HUMSearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// Add the searchBar and offset the table view so it's hidden by default.
self.tableView.tableHeaderView = self.searchBar;
self.tableView.contentOffset = CGPointMake(0.0, self.searchBar.frame.size.height);

这里似乎没有什么异常,但是在显示SearchDisplayController时,原始的tableview上有一个20像素的间隙。在这个例子中,我创建了一个子类SearchDisplayController,不隐藏NavigationBar,以便更清楚地了解发生了什么。

Gap

一个转换的视频捕获:http://cl.ly/0y3L0Z1R1I0c/20pixels.mov

我尝试过的事情:

  1. 在searchDisplayControllerWillBeginSearch时更改我的UITableView的框架以将其上移20px。这会破坏转换。http://cl.ly/033q0L3G0p1T/origin.mov
  2. 更改searchDisplayControllerWillBegin上的UITableView上的滚动偏移量。同样,这会破坏转换。
  3. 尝试手动动画UISearchBar,但我也无法使其工作。

有什么想法吗?

3个回答

3

经过多次尝试和错误,最终我成功实现了所需的行为。需要注意的是,在我的情况下,当搜索栏被点击时,我希望导航栏被推上去。以下解决方案修复了20像素间隙问题。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ...        
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.searchBar.clipsToBounds = YES;
    self.navigationController.navigationBar.translucent = YES;
}

viewWillDisappear方法中,需要将translucent属性设置为NO,否则在segue视图控制器中,导航栏会与视图内容一起被推出。
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
    self.navigationController.navigationBar.translucent = NO;
}

希望这能帮助到你。

2
如果不想让导航栏变成半透明,可以使用以下代码:
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = NO;
self.extendedLayoutIncludesOpaqueBars = YES;
- BreadicalMD

0

\me 向Aaron挥手

在我不断地撞墙后,这是最终为我们工作的解决方案。请注意,我们将导航栏与搜索栏一起显示。

注意:由于它基于我使用Reveal发现的一些东西进行视图潜水,因此这很可能会在iOS 8上出现问题,因此如果有人有一个更不易碎的解决方案,我很乐意听取建议。

- (UIView *)wrapperViewForTableView:(UITableView *)tableView
{
    //This was discovered via Reveal. May change for iOS 8.
    return tableView.subviews[1];
}

- (CGFloat)statusBarHeight
{
    return CGRectGetHeight([[UIApplication sharedApplication] statusBarFrame]);
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
    //Don't animate - this bit works without animation. 
    UIView *wrapperView = [self wrapperViewForTableView:self.tableView];
    CGRect frame = wrapperView.frame;
    frame.origin.y -= [self statusBarHeight];
    frame.size.height += [self statusBarHeight];

    wrapperView.frame = frame;
}

- (void)searchDisplayControllerWillEndSearch:(HUMSearchDisplayController *)controller
{
    //Animate this so it goes with the dismissal.
    [UIView animateWithDuration:0.25 animations:^{
        UIView *wrapperView = [self wrapperViewForTableView:self.tableView];
        CGRect frame = wrapperView.frame;
        frame.origin.y += [self statusBarHeight];
        frame.size.height -= [self statusBarHeight];
        wrapperView.frame = frame;
    }];
}

0

不要直接将UISearchBar分配为tableHeaderView,而是创建一个与UISearchBar相同大小的空UIView,并将UISearchBar作为此视图的子视图添加到其中,然后将其分配为tableHeaderView

这将防止在UISearchBar开始编辑时将UITableView向下推。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接