【问题标题】:UISearchBar to UISearchDisplayController transition is off by 20pxUISearchBar 到 UISearchDisplayController 的转换关闭了 20px
【发布时间】:2014-05-05 19:29:38
【问题描述】:

我正在 UITableView 的顶部实现 UISearchBar。

在我的 ViewDidLoad 中,我设置了 self.edgesForExtendedLayout = UIRectEdgeNone

以下是我添加 UISearchBar 的方式。我只是在 UITableView 的标题中添加一个 UISearchBar 并调整内容偏移量:

// 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 上有 20px 的间隙。在此示例中,我将 SearchDisplayController 子类化为不隐藏 NavigationBar 以更清楚地了解正在发生的事情。

转换的视频截图:http://cl.ly/0y3L0Z1R1I0c/20pixels.mov

我尝试过的事情:

  1. 在 searchDisplayControllerWillBeginSearch 上更改我的 UITableView 的框架,将其向上移动 20 像素。这打破了过渡。 http://cl.ly/033q0L3G0p1T/origin.mov
  2. 在 searchDisplayControllerWillBegin 上更改 UITableView 的滚动偏移量。同样,这也会中断过渡。
  3. 尝试手动为 UISearchBar 设置动画,但我也无法让它工作。

有什么想法吗?

【问题讨论】:

    标签: ios objective-c uisearchbar uisearchdisplaycontroller


    【解决方案1】:

    经过几次反复试验,我终于实现了预期的行为。请注意,在单击搜索栏时,我希望将导航栏向上推。以下解决方案修复了 20px 间隙问题。

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

    希望这会有所帮助。

    【讨论】:

    • 对于不使导航栏半透明的相同解决方案,请使用以下命令:self.edgesForExtendedLayout = UIRectEdgeAll; self.automaticallyAdjustsScrollViewInsets = NO; self.extendedLayoutIncludesOpaqueBars = YES;
    【解决方案2】:

    \我waves at Aaron

    在我的头撞到墙上后,这就是最终为我们工作的东西。请注意,我们保持导航栏与搜索栏一起显示。

    注意:这很可能会在 iOS 8 上中断,因为它会根据我使用 Reveal 发现的一些东西进行视图潜水,所以如果有人有一个不那么脆弱的解决方案,我很想听听.

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

    【讨论】:

      【解决方案3】:

      不要将UISearchBar 直接指定为tableHeaderView,而是创建一个与UISearchBar 大小相同的空UIView,并将UISearchBar 作为子视图添加到此视图,您将其指定为tableHeaderView.

      这将防止UITableViewUISearchBar 开始编辑时被下推。

      【讨论】:

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