【问题标题】:UITableView extra space at bottomUITableView 底部有多余空间
【发布时间】:2015-04-22 20:35:36
【问题描述】:

我的 tableview 在底部有一个额外的空间,如图所示:

tableView 中的所有行的高度都是固定的 71pt。

【问题讨论】:

标签: ios uitableview storyboard


【解决方案1】:

好的,我知道了。

在我的 viewController(不是我的 tableView)上标记了“调整滚动视图插图”。

这里是answer,有详细解释

【讨论】:

    【解决方案2】:

    在我的例子中 tableView 是通过编程创建的,我必须设置 tableView.estimatedRowHeight 以使空间消失

    【讨论】:

      【解决方案3】:

      @urgentx 的answer 完成了我想要的大部分工作,但并没有完全实现。我处于类似的情况(倒置的 UITableView 用于聊天应用程序)但是他们的建议完全禁用了安全区域行为,这在 iPhone X 等设备上是不可取的,因为这意味着桌面视图将被主页指示器和任何顶部导航栏。

      为了让表格视图在倒置时仍能正确避开安全区域,我做了以下操作:

      override func viewDidLoad() {
          super.viewDidLoad()
          self.automaticallyAdjustsScrollViewInsets = false
          self.tableView.contentInsetAdjustmentBehavior = .never
      }
      

      然后:

      override func viewWillLayoutSubviews() {
          super.viewWillLayoutSubviews()
          //fetch the safe area in layoutSubviews since this it's possible that they can change when the device is rotated
          let safeArea = self.tableView.safeAreaInsets
          //insets are **flipped** because the tableview is flipped over its Y-axis!
          self.tableView.contentInset = .init(top: safeArea.bottom, left: 0, bottom: safeArea.top, right: 0)
      }
      

      这实质上复制了contentInsetAdjustmentBehavior = .automatic 的行为,但插图在 Y 轴上翻转。

      【讨论】:

        【解决方案4】:

        在我的情况下,这是由于默认情况下在 UITableView 底部有一个高度为 18 的页脚。将其设置为 1 会删除底部的多余空间。

        【讨论】:

          【解决方案5】:

          这就是它可以通过 Storyboard (iOS 11) 轻松修复的方法:

          选择表格视图 > 大小检查器 > 内容插入:从不

          【讨论】:

            【解决方案6】:

            我遇到了同样的问题,最新的 Xcode (11.3) 和最新的 iOS (13.3) iPhone。我尝试了很多答案,但都没有奏效。

            我的tableView 有顶部、底部、前导和尾部约束,但不知何故,在最后一个单元格下方的底部有很大空间。 (tableView.contentSize.height 几乎是所需数据的两倍)tableView 的数据根本不是动态的。当拥有它的视图控制器显示它和 tableView 被隐藏然后再次显示时都会发生这种情况。

            我通过使用如下scrollView的委托方法解决了这个问题,这样当用户开始拖动时,高度就会被调整。对我来说,它非常有效,希望你也一样!

            // this is the best place to adjust the contentSize.height of tableView.
            func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
                print("scrollViewWillBeginDragging():") // optional
                // replace allParkingGeos.count with your data for tableView
                tableView.contentSize.height = CGFloat(allParkingGeos.count) * cellHeight
            }
            

            【讨论】:

            • 感谢 Brian Hong,它运行良好。但是我还是很好奇这个问题。就我而言,我有一个表视图,其中包含从 SQLite 加载的数据。没有searchBar的时候可以正常工作,但是当我将searchBar添加为UITableview header时,就会出现这个问题。
            【解决方案7】:

            对于 iOS 11 和 Xcode 9,接受的解决方案不再是正确的解决方案。要达到类似的效果,您必须转到 Size Inspector,您可以在其中找到:

            你可以在代码中实现同样的效果:

            if #available(iOS 11.0, *) {
                scrollView.contentInsetAdjustmentBehavior = .never
            } else {
                automaticallyAdjustsScrollViewInsets = false
            }
            

            【讨论】:

              【解决方案8】:

              当我将 UITableView 倒置以显示聊天消息屏幕时,我遇到了这个问题。

              在我翻转表格视图之前,内容插图在顶部留下了一个空白区域,这样当我们一直向上滚动时,内容就不会被导航栏挡住。一旦我翻转它,插图就移到了底部。

              在 UITableView 的 Size Inspector 中将“Content Insets”更改为 Never,以在最后摆脱这个空间。

              【讨论】:

                【解决方案9】:

                如果有嵌入式导航控制器,选中“显示工具栏”会在视图控制器中创建底部间距。只需取消选中它。

                【讨论】:

                  【解决方案10】:

                  在这种情况下,您可以将 tableview 的底部空间 0 分配给其父视图,我认为它会起作用,或者您可以这样做,

                  tableView.tableFooterView = UIView();

                  或者你可以像下面这样,

                  较低的值应该是 1.0。

                  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
                      if(section == 0) {
                         return 6;
                      } else {
                         return 1.0;
                      }
                  }
                  
                  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
                      return 5.0;
                  }
                  
                  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
                      return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
                  }
                  
                  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
                      return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    答案并没有解决我的问题。 我有一个UISearchbar 作为tableHeaderView 导致空白(我不知道为什么)。我从情节提要的表格视图中删除了搜索栏,并在表格标题中添加了viewWillLayoutSubviews,然后问题就解决了。

                    override func viewWillLayoutSubviews() {
                        super.viewWillLayoutSubviews()
                        tableView.tableHeaderView = searchBar
                    }
                    

                    【讨论】:

                    • 在 viewWillLayoutSubviews() 中重新计算 tableView 的高度确实解决了这个问题。我不知道为什么,但谢谢你的想法!
                    猜你喜欢
                    • 2016-09-25
                    • 2021-01-13
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-03-08
                    • 1970-01-01
                    相关资源
                    最近更新 更多