gpt4 book ai didi

iphone - 如何让 UITableView 始终位于 UIView 的中心?

转载 作者:IT王子 更新时间:2023-10-29 08:15:14 26 4
gpt4 key购买 nike

我想在 UIView 的中心实现一个 UITableView。最初它只有 2 或 3 行。当用户添加更多行时,它会在垂直方向延伸,而整个内容保持在中心,如下所示:

enter image description here

是否可以用 UITableView 做到这一点?

最佳答案

可以使用 UIScrollView 的 contentOffset 属性来完成。

  1. 让您的 tableView 的框架位于边界内:

    tableView.frame = self.view.bounds;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  2. 声明一个方法-layoutTableView:

    - (void)layoutTableView {
    CGSize contentSize = tableView.contentSize;
    CGSize boundsSize = tableView.bounds.size;
    CGFloat yOffset = 0;
    if(contentSize.height < boundsSize.height) {
    yOffset = floorf((boundsSize.height - contentSize.height)/2);
    }
    tableView.contentOffset = CGPointMake(0, yOffset);
    }
  3. 当您调用[tableView reloadData]时,只需调用[self layoutTableView]即可。

关于iphone - 如何让 UITableView 始终位于 UIView 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14031714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com