UISearchBar到UISearchDisplayController的转换是20px

Aar*_*key 5 objective-c uisearchbar uisearchdisplaycontroller ios

我正在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);
Run Code Online (Sandbox Code Playgroud)

看起来似乎没有任何异常,但是当显示SearchDisplayController时,原始tableview上有20px的差距.在这个例子中,我将SearchDisplayController子类化为不隐藏NavigationBar以使其更清楚发生了什么.

间隙

转型的视频捕捉:http://cl.ly/0y3L0Z1R1I0c/20pixels.mov

我试过的东西:

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

有任何想法吗?

Erg*_*ruh 3

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

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

半透明属性需要设置为viewWillDisappearNO,否则在segue视图控制器中导航栏仍然与视图内容一起推动。

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
    self.navigationController.navigationBar.translucent = NO;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

  • 对于不使导航栏半透明的相同解决方案,请使用以下内容: `self.edgesForExtendedLayout = UIRectEdgeAll;` self.automaticallyAdjustsScrollViewInsets = NO;` self.extendedLayoutInincludesOpaqueBars = YES;` (2认同)