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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31722758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:35:08  来源:igfitidea点击:

How to vertical align (center) the content of UITableView?

iosobjective-cxcodeuitableviewstoryboard

提问by Xchord

I want to center the content of my UITableViewthat contains headerViewand footerViewcreated at storyboard, and UITableViewCells. How can I achieve this?

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

Here is what I'm trying to implement to solve my problem but this does not work.

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

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

So I subtracted the height of the navigationBar & statusBar and cells' height to the tableView's height to get the height of the empty area. Now that I get the height if the empty area, I divided it to 2 for the footer and header's view.

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

回答by Pipiks

In the viewWillAppearand in the didRotateFromInterfaceOrientationfunctions :

viewWillAppeardidRotateFromInterfaceOrientation函数中:

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

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

This will solve your problem.

这将解决您的问题。

EDIT

编辑

Call the updateTableViewContentInsetfunction in the viewWillLayoutSubviewsand after each reloadData:

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

Ojective-C

目标-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

斯威夫特 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)
}

回答by Axel Guilmin

Override viewDidLayoutSubviewsand compare the tableView's framewith its contentSizeto set its contentInset. In Swift 4 :

覆盖viewDidLayoutSubviews并比较 tableViewframe与它的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)
}

This works well with self-sizing table view cells

这适用于自调整表格视图单元格

回答by Aerows

Use the contentSizeproperty of UITableView, that how you don't have to take the cell heightor countinto account.

使用 的contentSize属性UITableView,您不必考虑单元格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);
}

Update the contentInsetsafter reloading tableView-data, and when the containing view of the tableViewbecomes visible (viewWillAppear:) and when it changes size (viewWillTransitionToSize:withTransitionCoordinator:).

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

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

回答by GregP

Swift 3 based on Pipiks answer

基于 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)

回答by dory daniel

for SWIFT 3 :

对于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)
}

RESULT :

结果 :

enter image description hereenter image description hereenter image description hereenter image description here

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

回答by Ersin Sezgin

Add a UIViewat the top (UIView should be parent of your TableView) of your TableViewfrom storyboard. After that set its constraints to fill the view. Now, set the size of your TableView and set its constraints to center both vertically and horizontally your top UIView. This will solve your problem.

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

回答by Xchord

I found out what causing my computation wrong. The value of self.tableView.bounds.size.height is different from the actual height in viewWillAppear. I used self.view.bounds.size.height instead.

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

回答by Lalit Kumar

Set frame of your UITableView like this

像这样设置 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);

or try this

或者试试这个

enter image description here

在此处输入图片说明