gpt4 book ai didi

ios - 如何垂直对齐(居中)UITableView 的内容?

转载 作者:技术小花猫 更新时间:2023-10-29 10:41:42 27 4
gpt4 key购买 nike

我想将包含在 Storyboard中创建的 headerViewfooterView 以及 UITableViewCell 的内容居中>s。我怎样才能做到这一点?

这是我试图实现以解决我的问题的方法,但这不起作用。

- (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;
}

所以我将导航栏和状态栏的高度以及单元格的高度减去tableView的高度以获得空白区域的高度。现在我得到了空白区域的高度,我将它分成 2 用于页脚和页眉的 View 。

最佳答案

viewWillAppeardidRotateFromInterfaceOrientation 函数中:

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

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

这将解决您的问题。

编辑

viewWillLayoutSubviews 中调用 updateTableViewContentInset 函数并在每次 reloadData 之后:

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);
}

swift 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)
}

关于ios - 如何垂直对齐(居中)UITableView 的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722758/

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