【问题标题】:How to vertical align (center) the content of UITableView?如何垂直对齐(居中)UITableView 的内容?
【发布时间】:2015-10-21 18:01:14
【问题描述】:

我想将包含在故事板创建的headerViewfooterView 以及UITableViewCells 的UITableView 的内容居中。我怎样才能做到这一点?

这是我正在尝试实施以解决我的问题,但这不起作用。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    CGFloat height = self.tableView.frameHeight - self.navigationController.navigationBar.frameHeight - [UIApplication sharedApplication].statusBarFrame.size.height - (self.rowCount * self.rowHeight);
    self.tableView.tableHeaderView.frameHeight = height / 2.0;
}

所以我将 navigationBar & statusBar 的高度和单元格的高度减去 tableView 的高度,得到空白区域的高度。 现在我得到了空白区域的高度,我将它划分为 2 用于页脚和页眉的视图。

【问题讨论】:

  • 动态设置表格视图的框架
  • 粘贴一些代码你试过什么?
  • @LalitKumar,我更新了这个问题。谢谢。
  • 中心是什么意思?
  • 你能添加你想要的图片吗?

标签: ios objective-c xcode uitableview storyboard


【解决方案1】:

viewWillAppeardidRotateFromInterfaceOrientation 函数中:

CGFloat headerHeight = (self.view.frame.size.height - (ROW_HEIGHT * [self.tableView numberOfRowsInSection:0]))) / 2;

self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0);

这将解决您的问题。

编辑

viewWillLayoutSubviews 和每个 reloadData 之后调用 updateTableViewContentInset 函数:

Objective-C

- (void)updateTableViewContentInset {
    CGFloat viewHeight = self.view.frame.size.height;
    CGFloat tableViewContentHeight = self.tableView.contentSize.height;
    CGFloat marginHeight = (viewHeight - tableViewContentHeight) / 2.0;

    self.tableView.contentInset = UIEdgeInsetsMake(marginHeight, 0, -marginHeight, 0);
}

斯威夫特 4

func updateTableViewContentInset() {
    let viewHeight: CGFloat = view.frame.size.height
    let tableViewContentHeight: CGFloat = tableView.contentSize.height
    let marginHeight: CGFloat = (viewHeight - tableViewContentHeight) / 2.0

    self.tableView.contentInset = UIEdgeInsets(top: marginHeight, left: 0, bottom:  -marginHeight, right: 0)
}

【讨论】:

  • 您的解决方案非常优雅和干净,在您拥有动态数量的单元格的情况下会失败。如果您的表格有太多的单元格,它将无法正确显示。所以下面将解决这个问题:headerHeight = MAX(0, headerHeight);
  • 为什么不使用self.tableView.rowHeight 而不是ROW_HEIGHT
  • 在 ios 12 中测试,swift 4.2 我必须添加:tv.rowHeight = UITableView.automaticDimension & tv.estimatedRowHeight = UITableView.automaticDimension
  • 如果有调整的内容插入怎么办?
【解决方案2】:

覆盖viewDidLayoutSubviews 并将tableView 的frame 与其contentSize 进行比较以设置其contentInset。在 Swift 4 中:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let tableViewHeight = self.tableView.frame.height
    let contentHeight = self.tableView.contentSize.height

    let centeringInset = (tableViewHeight - contentHeight) / 2.0
    let topInset = max(centeringInset, 0.0)

    self.tableView.contentInset = UIEdgeInsets(top: topInset, left: 0.0, bottom: 0.0, right: 0.0)
}

这适用于自调整大小的表格视图单元格?

【讨论】:

  • 为了实现真正的 Swift 4,将最后一行更改为:self.tableView.contentInset = UIEdgeInsets(top: topInset, left: 0.0, bottom: 0.0, right: 0.0)
  • @RaulHuerta :确实 'UIEdgeInsetsMake' 已被 'UIEdgeInsets.init(top:left:bottom:right:) 取代,我更新了我的答案
  • viewDidLayoutSubviews 中,self.tableView.contentSize.height 是从estimatedRowHeight 计算出来的,而不是实际调整大小的单元格。将其移至viewDidAppear 即可。但不确定这是否是一个好的解决方案。
【解决方案3】:

使用UITableViewcontentSize 属性,您不必考虑单元格heightcount

- (void) updateTableInsetsForHeight: (CGFloat) height
{
    CGFloat tableViewInset = MAX((height - self.tableView.contentSize.height) / 2.0, 0.f);
    self.tableView.contentInset = UIEdgeInsetsMake(tableViewInset, 0, -tableViewInset, 0);
}

在重新加载tableView-数据后更新contentInsets,当tableView 的包含视图变得可见时(viewWillAppear:) 以及当它改变大小时(viewWillTransitionToSize:withTransitionCoordinator:)。

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [self updateTableInsetsForHeight: size.height];
}

【讨论】:

    【解决方案4】:

    基于 Pipiks 答案的 Swift 3

    let headerHeight: CGFloat = (view.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0))) / 2
    tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0)
    

    【讨论】:

      【解决方案5】:

      在情节提要中TableView 的顶部(UIView 应该是 TableView 的父级)添加一个 UIView。之后设置其约束以填充视图。现在,设置 TableView 的大小并将其约束设置为垂直和水平居中您的顶部 UIView。这将解决您的问题。

      【讨论】:

        【解决方案6】:

        我发现了导致我的计算错误的原因。 self.tableView.bounds.size.height 的值与 viewWillAppear 中的实际高度不同。我改用 self.view.bounds.size.height。

        【讨论】:

          【解决方案7】:

          对于SWIFT 3:

          var headerHeight: CGFloat = (tableView.frame.size.height - CGFloat(Int(tableView.rowHeight) * tableView.numberOfRows(inSection: 0))) / 2
          if headerHeight > 0 {
              tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0)
          } else {
              headerHeight = 0
              tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, 0/*-headerHeight*/, 0)
          }
          

          结果:

          【讨论】:

            【解决方案8】:

            像这样设置 UITableView 的框架

            CGFloat width = self.view.frame.size.width;
            CGFloat height = self.view.frame.size.height;
            CGFloat tableHeight = 200.0; //your table height
            self.tableView.frame = CGRectMake(0,(height-tableHeight)/2,width,tableHeight);
            

            或者试试这个

            【讨论】:

            • 谢谢,但我不想更改 tableView 的框架,因为我正在使用自动布局。
            猜你喜欢
            • 2018-01-31
            • 1970-01-01
            • 2014-11-13
            • 2019-04-15
            • 1970-01-01
            • 2013-02-13
            • 2016-10-19
            • 1970-01-01
            相关资源
            最近更新 更多