gpt4 book ai didi

ios - 使 UISearchBar 不像在联系人应用程序中那样滚动

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:10 25 4
gpt4 key购买 nike

我正在尝试实现一个类似于 Contacts iOS 应用程序的 UI,其中有一个 UISearchBar 锚定在表格 View 的顶部(如果将其添加为表格 View 标题,它将随内容滚动),并且当您开始搜索时,搜索栏会占用导航栏的空间。

我正在使用 UISearchController 的搜索栏。

已尝试在 TableView 顶部添加容器 View ,并以编程方式向其添加搜索栏,但问题是当搜索栏转到导航栏的位置时(由 UISearchController 自动完成)它的宽度比屏幕大... enter image description here

这在模拟器和设备中都会发生。

有什么方法(最好是非 hacky)来完成这个?

最佳答案

不要使用 UITableViewController,使用标准的 UIViewController 并自己添加 searchBartableview

这是一个示例代码:

- (void)viewDidLoad
{
[super viewDidLoad];

UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.translatesAutoresizingMaskIntoConstraints = NO;
searchBar.delegate = self;
[searchBar sizeToFit];

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.delegate = self;
tableView.dataSource = self;
tableView.contentInset = UIEdgeInsetsMake(searchBar.frame.size.height, 0.0, 0.0, 0.0);
[tableView reloadData];

[self.view addSubview:tableView];
[self.view addSubview:searchBar];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:searchBar attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:tableView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:tableView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
}

关于ios - 使 UISearchBar 不像在联系人应用程序中那样滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149168/

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