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
How to vertical align (center) the content of UITableView?
提问by Xchord
I want to center the content of my UITableView
that contains headerView
and footerView
created at storyboard, and UITableViewCell
s. How can I achieve this?
我想将我在故事板UITableView
中包含headerView
和footerView
创建的内容和UITableViewCell
s. 我怎样才能做到这一点?
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 :
在viewWillAppear和didRotateFromInterfaceOrientation函数中:
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 viewDidLayoutSubviews
and compare the tableView's frame
with its contentSize
to 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 contentSize
property of UITableView
, that how you don't have to take the cell height
or count
into account.
使用 的contentSize
属性UITableView
,您不必考虑单元格height
或count
考虑。
- (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 contentInsets
after reloading tableView
-data, and when the containing view of the tableView
becomes 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 :
结果 :
回答by Ersin Sezgin
Add a UIView
at the top (UIView should be parent of your TableView) of your TableView
from 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.
UIView
在TableView
故事板的顶部(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 代替。