IOS:UISearchBar部分用提示符覆盖UINavigationBar(IOS: UISearchBar partly covering UINavigationBar with prompt)

编程入门 行业动态 更新时间:2024-10-24 17:24:41
IOS:UISearchBar部分用提示符覆盖UINavigationBar(IOS: UISearchBar partly covering UINavigationBar with prompt)

我正在使用带有UISearchBar的UITableViewController。 UITableViewController嵌入在UINavigationController中。 显示表格视图时,搜索栏按预期位于顶部(在导航栏下方)。 导航栏显示标题和提示。

当我单击搜索栏时,它会向上移动并隐藏导航栏中的标题。 提示仍然可见。 当我不使用提示时,搜索栏在我点击它之后仍然按照预期位于导航栏下方。

我创建了一个非常简单的项目来演示我的问题。 该项目具有UITableViewController的以下文件:

import UIKit class searchTVC: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating { fileprivate let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { super.viewDidLoad() title = "Title" navigationItem.prompt = "prompt" tableView.backgroundColor = UIColor.gray // Add search bar searchController.delegate = self searchController.searchResultsUpdater = self searchController.hidesNavigationBarDuringPresentation = false searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar // remove separators for empty table tableView.tableFooterView = UIView() } func updateSearchResults(for searchController: UISearchController) { // ignore } }

在故事板中,只有一个空的UITableViewController嵌入在UINavigationController中。 结果如下所示。

如果您注释掉设置提示的行,则行为符合预期。

我最近将我的代码更新为Swift 4.我不记得在使用Swift 3时我看到了这种行为,但我不完全确定它是用Swift 4引入的。

我在代码中遗漏了什么,或者这是一个错误?

I am using a UITableViewController with a UISearchBar. The UITableViewController is embedded in a UINavigationController. When the table view is shown the search bar is at the top as expected (below the navigation bar). The navigation bar is showing a title and a prompt.

When I click the search bar, it is moved up and hides the title in my navigation bar. The prompt is still visible. When I do not use a prompt, the search bar is still below the navigation bar as expected after I clicked it.

I have created a very simple project to demonstrate my problem. The project has the following file for a UITableViewController:

import UIKit class searchTVC: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating { fileprivate let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { super.viewDidLoad() title = "Title" navigationItem.prompt = "prompt" tableView.backgroundColor = UIColor.gray // Add search bar searchController.delegate = self searchController.searchResultsUpdater = self searchController.hidesNavigationBarDuringPresentation = false searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar // remove separators for empty table tableView.tableFooterView = UIView() } func updateSearchResults(for searchController: UISearchController) { // ignore } }

In the storyboard there is only an empty UITableViewController that is embedded in an UINavigationController. The result is shown below.

If you comment out the line that sets the prompt the behavior is as expected.

I recently updated my code to Swift 4. I cannot recall that I saw this behavior when using Swift 3, but I am not completely sure that it was introduced with Swift 4.

Am I missing something in the code or is this a bug?

最满意答案

在viewDidLoad设置这些:

self.definesPresentationContext = true self.edgesForExtendedLayout = .bottom

Set these in viewDidLoad:

self.definesPresentationContext = true self.edgesForExtendedLayout = .bottom

更多推荐

本文发布于:2023-07-31 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1347274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提示符   UISearchBar   IOS   UINavigationBar   prompt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!