HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何垂直对齐(居中)UITableView的内容?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想集中我的UITableView的内容,其中包含在storyboard和UITableViewCells创建的headerView和footerView.我怎样才能做到这一点?

这是我试解决我的问题,但这不起作用.

- (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和cells的高度为tableView的高度,以获取空白区域的高度.
现在我得到了空白区域的高度,我将其分为2为页脚和标题的视图.

解决方法

在viewWillAppear和didRotateFromInterfaceOrientation函数中:
CGFloat headerHeight = (self.view.frame.size.height - (ROW_HEIGHT * [self.tableView numberOfRowsInSection:0]))) / 2;

self.tableView.contenTinset = UIEdgeInsetsmake(headerHeight,-headerHeight,0);

这将解决您的问题.

编辑

在viewWillLayoutSubviews中调用updateTableViewContenTinset函数,并在每次reloadData之后调用

Ojective-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,-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)
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何垂直对齐(居中)UITableView的内容?全部内容,希望文章能够帮你解决ios – 如何垂直对齐(居中)UITableView的内容?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。