按下“返回”后,TableView单元格被导航栏覆盖?

3

我有一个表格视图在导航栏下面,当用户在搜索栏中键入内容时,它将显示出来。一切似乎都正常。但是当我推出一个视图控制器并在tableview中选中单元格后,从detailViewController返回后,我的tableview就混乱了。

• 问题:在从detailViewController返回之后,tableview最顶部的单元格被导航栏遮挡。但是一开始不会发生这种情况。

• 我已经尝试过:

   1. Toggled 'Under Top Bars' in the storyboard
   2. Check all my constraint

这是将表格视图添加到子视图的方法。
func updateSearchResults(for searchController: UISearchController) {

    // When the user is typing
    if let searchText = searchController.searchBar.text, !searchText.isEmpty {

        // add the tableview onto the subview
        view.addSubview(tableView)

        self.searchController.dimsBackgroundDuringPresentation = false

        // filter the username array, to match the text in the search bar
        filteredUsernames = unfilterdUsernames.filter({ (username) -> Bool in
            return username.lowercased().contains(searchText.lowercased())
        })

        if let usernames = filteredUsernames {
            getUsersFromUsernames(usernames: usernames, completion: { (users) in
                self.filteredUsers = users
            })
        }

        // else, the user is not typing anything. Remove the tableview from the subview
    } else {
        self.searchController.dimsBackgroundDuringPresentation = false

        tableView.removeFromSuperview()
        filteredUsernames = unfilterdUsernames

    }
    // reload tableview data
    tableView.reloadData()
}

这是我进入detailViewController的方式:

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

在UITableView中,当用户选择某一行时,该方法会被调用。
// the profile view that I want to display, after the user select one of the tableViewCell
    let profileController = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "profile") as? ProfileCollectionViewController

    // user is the value that I want to transfer to the profileController
    // which user from the indexPath in tableView
    if let user = filteredUsers?[indexPath.row] {
        userToPass = user
        // set the delegate to self
        profileController?.userDataDelegate = self
        // push a navigationController to profileController
        self.navigationController?.pushViewController(profileController!, animated: true)

进入detailViewController前:

enter image description here

从detailViewController返回后:

第一个索引被导航控制器覆盖了

enter image description here


这一行真的在那里,而且被覆盖了吗?或者"babi"是表中唯一的一行吗?换句话说,当你从"个人资料"视图"回来"时,你能否下拉表格以查看顶部行(包含"jajang")? - DonMag
@DonMag 是的,顶部一行是“炸酱面”,但是在导航栏下方(从详细视图返回时按下返回按钮)覆盖了顶部一行。这意味着我仍然可以向下滚动。但一开始并不是这样的。 - Buka Cakrawala
它会在导航栏下方弹回。但我很确定我没有改变高度或约束任何东西。 - Buka Cakrawala
我建议:当您第一次看到表格并且它看起来正确时,请进入Debug View Hierarchy(调试视图层次结构)并注意约束和值。当您“回来”时,如果顶部行位于导航栏下方,请再次使用Debug View Hierarchy(调试视图层级)查找差异。此外...确保您的代码没有添加多个表视图。 - DonMag
所以在按返回按钮之前,“看起来表视图的” y 原点在导航栏下方的边界上。但是在按返回按钮后,表视图的 y 位置向上移动了,结果它位于导航栏后面。不知道为什么,我从未改变过位置。 - Buka Cakrawala
显示剩余2条评论
1个回答

0

前往故事板。

选择 ViewController(不是表视图或导航控制器)。

在属性检查器中,取消选中 调整滚动视图插图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接