gpt4 book ai didi

ios - UISearchBar 到 UISearchDisplayController 的转换关闭了 20px

转载 作者:可可西里 更新时间:2023-11-01 04:44:00 25 4
gpt4 key购买 nike

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

Gap

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

我尝试过的事情:

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

有什么想法吗?

最佳答案

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

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

viewWillDisappear 上,半透明属性需要设置为 NO,否则在 segue View Controller 中,导航栏将与 View 内容一起被推送。

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

希望这对您有所帮助。

关于ios - UISearchBar 到 UISearchDisplayController 的转换关闭了 20px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23480254/

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