如何设置tableHeaderView(UITableView的)与自动布局的高度? [英] How do I set the height of tableHeaderView (UITableView) with autolayout?

查看:1559
本文介绍了如何设置tableHeaderView(UITableView的)与自动布局的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直砸我的头靠在这个墙上去年3或4个小时,我似乎无法弄清楚。我有它内部的一个全屏幕的UITableView一个UIViewController(有画面,这就是为什么我不能使用的UITableViewController上一些其他的东西),我想找到自己的tableHeaderView与自动布局来调整。不用说,这不是合作。

请参阅下面的截图。

由于overviewLabel(例如列表概述此处。文)的动态内容,我使用自动布局来调整它,它是上海华。我把一切都调整很好,除了tableHeaderView,这是对下面Paralax表视图中hiearchy。

我发现来调整该标题视图的唯一途径是编程,具有以下code:

 的CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = headerFrameHeight;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];

在这种情况下,headerFrameHeight是tableViewHeader高度的手工计算如下的(innerHeaderView是白色区域,或第二查看,headerView是tableHeaderView)

  CGFloat的startingY = self.innerHeaderView.frame.origin.y + self.overviewLabel.frame.origin.y;
的CGRect overviewSize = [self.overviewLabel.text
                       boundingRectWithSize:CGSizeMake(290.f,CGFLOAT_MAX)
                       选项​​:NSStringDrawingUsesLineFragmentOrigin
                       属性:@ {NSFontAttributeName:self.overviewLabel.font}
                       背景:无];
CGFloat的overviewHeight = overviewSize.size.height;
CGFloat的overviewPadding =([self.overviewLabel.text长度]&0)? 10:0; //如果没有overviewText,消除整体高度的填充。
CGFloat的headerFrameHeight = ceilf(startingY + overviewHeight + overviewPadding + 21.f + 10.f);

手工计算工作,但如果事情在将来改变它的笨重又容易出错。我希望能够做的是有根据提供的约束tableHeaderView自动调整大小,就像你可以在任何地方别的。但对我的生活,我不能明白。

有就中越关于这几个职位,但没有清晰,结束了更多的困惑我。这里有几个:


  • 在UITableViewHeader

  • 自动布局

  • 自动布局tableHeaderView


  • <一个href=\"http://stackoverflow.com/questions/16471846/is-it-possible-to-use-autolayout-with-uitableviews-tableheaderview\">Is可以用的UITableView的tableHeaderView使用自动版式?


  • <一个href=\"http://stackoverflow.com/questions/19005446/table-header-view-height-is-wrong-when-using-auto-layout-ib-and-font-sizes\">table使用自动布局,IB和字体大小

  • 在当前视图头部高度是错误的

它并没有真正意义的改变translatesAutoresizingMaskIntoConstraints财产为NO,因为这只是造成错误,我和没有意义的概念呢。

任何帮助真的会AP preciated!

编辑1:
由于TomSwift的建议下,我能弄明白。不用手动计算概述的高度,我可以把它计算了我如下,然后像以前一样重新设置tableHeaderView。

  [self.headerView setNeedsLayout]
[self.headerView layoutIfNeeded]
CGFloat的高度= [self.innerHeaderView systemLayoutSizeFittingSize:UILayoutFittingCom pressedSize] .height + self.innerHeaderView.frame.origin.y; //添加由来是因为innerHeaderView开始中途下来headerView。的CGRect headerFrame = self.headerView.frame;
headerFrame.size.height =高度;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];

编辑2:正如其他人所指出的,张贴在编辑1解决方案似乎并不在viewDidLoad中工作。它,然而,似乎在viewWillLayoutSubviews工作。下面的例子code:

  //注1:变量名称下方不符合上述变量 - 这是旨在成为一个简化的最终的答案。
//注2:_headerView是pviously分配给tableViewHeader(在的loadView在我的情况,因为我现在做的一切编程)$ P $。
//注3:的autoLayout code可以设置在编程updateViewConstraints。
- (无效){viewWillLayoutSubviews
    [超级viewWillLayoutSubviews]    [_headerWrapper setNeedsLayout]
    [_headerWrapper layoutIfNeeded]
    CGFloat的高度= [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCom pressedSize] .height;    的CGRect headerFrame = _headerWrapper.frame;
    headerFrame.size.height =高度;
    _headerWrapper.frame = headerFrame;
    _tableView.tableHeaderView = _headerWrapper;
}


解决方案

您需要使用的UIView systemLayoutSizeFittingSize:方法来获得你的头视图的最小边界大小。

我提供了进一步讨论这个Q / A使用这个API:

<一个href=\"http://stackoverflow.com/questions/18118021/how-to-resize-superview-to-fit-all-subviews-with-autolayout/18155803#18155803\">How重新调整上海华适合所有子视图与自动布局?

I'm been smashing my head against the wall with this for last 3 or 4 hours and I can't seem to figure it out. I have a UIViewController with a full screen UITableView inside of it (there's some other stuff on the screen, which is why I can't use a UITableViewController) and I want to get my tableHeaderView to resize with autolayout. Needless to say, it's not cooperating.

See screenshot below.

Because the overviewLabel (e.g. the "List overview information here." text) has dynamic content, I'm using autolayout to resize it and it's superview. I've got everything resizing nicely, except for the tableHeaderView, which is right below Paralax Table View in the hiearchy.

The only way I've found to resize that header view is programatically, with the following code:

CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = headerFrameHeight;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];

In this case, headerFrameHeight is a manual calculation of the tableViewHeader height as follows (innerHeaderView is the white area, or the second "View", headerView is tableHeaderView):

CGFloat startingY = self.innerHeaderView.frame.origin.y + self.overviewLabel.frame.origin.y;
CGRect overviewSize = [self.overviewLabel.text
                       boundingRectWithSize:CGSizeMake(290.f, CGFLOAT_MAX)
                       options:NSStringDrawingUsesLineFragmentOrigin
                       attributes:@{NSFontAttributeName: self.overviewLabel.font}
                       context:nil];
CGFloat overviewHeight = overviewSize.size.height;
CGFloat overviewPadding = ([self.overviewLabel.text length] > 0) ? 10 : 0; // If there's no overviewText, eliminate the padding in the overall height.
CGFloat headerFrameHeight = ceilf(startingY + overviewHeight + overviewPadding + 21.f + 10.f);

The manual calculation works, but it's clunky and prone to error if things change in the future. What I want to be able to do is have the tableHeaderView auto-resize based on the provided constraints, like you can anywhere else. But for the life of me, I can't figure it out.

There's several posts on SO about this, but none are clear and ended up confusing me more. Here's a few:

It doesn't really make sense to change the translatesAutoresizingMaskIntoConstraints property to NO, since that just causes errors for me and doesn't make sense conceptually anyway.

Any help would really be appreciated!

EDIT 1: Thanks to TomSwift's suggestion, I was able to figure it out. Instead of manually calculating the height of the overview, I can have it calculated for me as follows and then re-set the tableHeaderView as before.

[self.headerView setNeedsLayout];
[self.headerView layoutIfNeeded];
CGFloat height = [self.innerHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + self.innerHeaderView.frame.origin.y; // adding the origin because innerHeaderView starts partway down headerView.

CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = height;
self.headerView.frame = headerFrame;
[self.listTableView setTableHeaderView:self.headerView];

Edit 2: As others have noted, the solution posted in Edit 1 doesn't seem to work in viewDidLoad. It does, however, seem to work in viewWillLayoutSubviews. Example code below:

// Note 1: The variable names below don't match the variables above - this is intended to be a simplified "final" answer.
// Note 2: _headerView was previously assigned to tableViewHeader (in loadView in my case since I now do everything programatically).
// Note 3: autoLayout code can be setup programatically in updateViewConstraints.
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    [_headerWrapper setNeedsLayout];
    [_headerWrapper layoutIfNeeded];
    CGFloat height = [_headerWrapper systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    CGRect headerFrame = _headerWrapper.frame;
    headerFrame.size.height = height;
    _headerWrapper.frame = headerFrame;
    _tableView.tableHeaderView = _headerWrapper;
}

解决方案

You need to use the UIView systemLayoutSizeFittingSize: method to obtain the minimum bounding size of your header view.

I provide further discussion on using this API in this Q/A:

How to resize superview to fit all subviews with autolayout?

这篇关于如何设置tableHeaderView(UITableView的)与自动布局的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆