ios - 如果我更改约束,Xcode 搜索栏不在表标题 View 中

标签 ios xcode swift tableview uisearchbar

我想在表格 View 的顶部制作一个搜索栏 我想在单击按钮时更改 TableView 的位置。

(我用这段代码制作了搜索栏)

searchController = UISearchController(searchResultsController: nil)   
table.tableHeaderView = searchController.searchBar

所以我改变了约束并且它移动了。

但问题是如果搜索处于事件状态,搜索栏不会跟随表格 View 。如果我点击取消按钮,它就会随之而来。

为什么会这样?有什么解决办法吗?

最佳答案

您是如何创建搜索栏的?我正在使用它并像这样创建:

Search Bar example

我已将 Table View 添加到我的场景中,并向其添加了 Search Bar 和 Search Display Controller。我还在我的 viewDidLoad 中添加了以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.processTableView setDelegate:self];
    [self.processTableView setDataSource:self];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.processTableView.tableHeaderView = self.searchController.searchBar;

    self.definesPresentationContext = YES;
}

像这样使用它,我得到了搜索栏的预期行为。这是您想要实现的目标吗?

enter image description here

关于ios - 如果我更改约束,Xcode 搜索栏不在表标题 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975394/

相关文章:

swift - 适用于 ios 的 google placepicker 和 BackgroundMapViewController.swift

ios - 在 iOS 中如何使用 alert 来提示输入文本?

iphone - 使用 UIImagePickerController 偷偷录制电影

xcode - Xcode 如何决定用于自动完成的预处理器宏和其他build设置?

ios - SwiftUI 自定义 List 不能在 ForEach 中使用 Binding

ios - 如何将 ImageSlideshow 库与 sdwebimage 库一起使用

ios - 选择单元格或滚动时 UITableView 上的 UIImage 移动

ios - 我正在寻找一种更短/更好的方法来从我的核心数据应用程序中获取第一个对象和 NSSet

ios - 文本字段标签和表格 View

iphone - 如果 AlertView 则不要在 Xcode 中弹出导航 Controller