我以编程方式向我的 tableHeaderView 添加了一个 UISearchBar,但它没有在 View 加载时显示。 我的表格内容上方只有一个空白区域。当我向下滚动一点时,它出现了。 我更改了 tableHeaderView 的背景颜色以查看它是否可见 - 我可以看到更改后的颜色,因此 searchBar 本身似乎是不可见的。 有什么想法吗?
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
...
// Initialize the refresh control.
[self.refreshControl addTarget:self
action:@selector(refreshData)
forControlEvents:UIControlEventValueChanged];
// Suchelemente erzeugen
self.searchBar = [UISearchBar new];
self.searchDC = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDC.searchResultsDelegate = self;
self.searchDC.delegate = self;
self.searchDC.searchResultsDataSource = self;
self.tableView.tableHeaderView = self.searchBar;
// some attempts to make it visible
self.searchBar.hidden = NO;
self.searchBar.layer.hidden = NO;
[self.searchBar sizeToFit];
}
好的,发现问题出在刷新控件上。 在 viewWillAppear 中调用了一个方法来更新刷新控件的标题。当我评论这一行时,搜索栏出现:
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title];
那么,如何让它们同时工作呢??
最佳答案
您可以通过以下方式设置它:
self.navigationItem.hidesSearchBarWhenScrolling = NO;
关于ios - UISearchBar 在向下滚动之前不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28126596/