ios 是否可以使用自动布局获取动态表格视图部分标题高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29462331/
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-08-31 05:30:14  来源:igfitidea点击:

Is it possible to obtain a dynamic table view section header height using Auto Layout?

iosuitableviewios8autolayout

提问by Jordan H

New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the content increases in height, the cell will also increase in height. This is extremely useful, and am wondering if the same feat can be accomplished for section headers in a table view?

iOS 8 中的新功能,您可以通过简单地设置估计的行高来获得 100% 动态表格视图单元格,然后使用自动布局在单元格中布局您的元素。如果内容的高度增加,单元格的高度也会增加。这非常有用,我想知道是否可以为表格视图中的节标题完成同样的壮举?

Can one, for example, create a UIViewin tableView:viewForHeaderInSection:, add a UILabelsubview, specify auto layout constraints for the label against the view, and have the view increase in height to fit the label's contents, without having to implement tableView:heightForHeaderInSection:?

例如,可以创建一个UIViewin tableView:viewForHeaderInSection:,添加一个UILabel子视图,为视图指定标签的自动布局约束,并增加视图的高度以适应标签的内容,而无需实现tableView:heightForHeaderInSection:

The documentation for viewForHeaderInSectionstates: "This method only works correctly when tableView:heightForHeaderInSection: is also implemented." I haven't heard if anything has changed for iOS 8.

文档viewForHeaderInSection说明:“此方法仅在 tableView:heightForHeaderInSection: 也实现时才能正常工作。” 我还没有听说 iOS 8 是否有任何变化。

If one cannot do that, what is the best way to mimic this behavior?

如果不能做到这一点,模仿这种行为的最佳方法是什么?

回答by Jordan H

This is possible. It is new right alongside the dynamic cell heights implemented in iOS 8.

这个有可能。它与 iOS 8 中实现的动态单元格高度一起是新的。

It's very simple. Just add this in viewDidLoad:

这很简单。只需将其添加到viewDidLoad

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;

Then override viewForHeaderInSectionand use Auto Layout to constrain elements in your view as seen fit. That's it! No need to implement heightForHeaderInSection. And actually the sectionHeaderHeightdoesn't need not be stated either, I just added it for clarity.

然后覆盖viewForHeaderInSection并使用自动布局来限制您认为合适的视图中的元素。就是这样!无需执行heightForHeaderInSection。实际上sectionHeaderHeight也不需要说明,我只是为了清楚起见添加了它。

Note that in iOS 11, cells and header/footer views use estimated heights by default. The only thing to do is provide an estimated height to better inform the system what to expect. The default value is UITableViewAutomaticDimensionbut you should provide a better estimate that is the average height they will be if possible.

请注意,在 iOS 11 中,单元格和页眉/页脚视图默认使用估计高度。唯一要做的就是提供估计的高度,以便更好地通知系统预期的结果。默认值是,UITableViewAutomaticDimension但您应该提供更好的估计值,即如果可能的话,它们将是平均高度。

回答by Clay Ellis

This can be accomplished by setting (or returning) the estimatedSectionHeaderHeighton your table view.

这可以通过estimatedSectionHeaderHeight在表视图上设置(或返回)来实现。

If your section header is overlapping your cells after setting estimatedSectionHeaderHeight, make sure that you're using an estimatedRowHeightas well.

如果设置后部分标题与单元格重叠,请estimatedSectionHeaderHeight确保您也使用estimatedRowHeight

(I'm adding this answer because the second paragraph contains an answer to an issue that can be found after reading through all of the comments which some might miss.)

(我添加这个答案是因为第二段包含一个问题的答案,在阅读了一些可能会错过的所有评论后可以找到该问题的答案。)

回答by Ahsan Ebrahim

Got stuck in the same issue where header was getting zero height untill and unless I provide a fixed height in the delegate for heighForHeaderInSection.

陷入了同样的问题,其中 header 的高度为零,除非我在委托中为heighForHeaderInSection.

Tried a lot of solutions which includes

尝试了很多解决方案,其中包括

self.tableView.sectionHeaderHeight = UITableView.automaticDimension
self.tableView.estimatedSectionHeaderHeight = 73`

But nothing worked. My cell were using proper autolayouts too. Rows were changing their height dynamically by using the following code but section header weren't.

但没有任何效果。我的手机也使用了正确的自动布局。行通过使用以下代码动态更改其高度,但部分标题没有。

self.tableView.estimatedRowHeight = 135
self.tableView.rowHeight = UITableView.automaticDimension

The fix is extremely simple and weird too but I had to implement the delegate methods instead of 1 line code for the estimatedSectionHeaderHeightand sectionHeaderHeightwhich goes as follows for my case.

解决方法是非常简单和太怪异,但我不得不执行的委托方法,而不是1个代码,estimatedSectionHeaderHeightsectionHeaderHeight肚里如下我的情况。

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    UITableView.automaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    73
}

回答by Mr.Javed Multani

Swift 4+

斯威夫特 4+

working(Tested 100%)

工作(测试 100%)

If you need both section as well row with dynamic height based on content then you can use below code:

如果您需要两个部分以及基于内容的动态高度行,那么您可以使用以下代码:

On viewDidLoad() write this lines:

在 viewDidLoad() 上写下这几行:

    self.globalTableView.estimatedRowHeight = 20
    self.globalTableView.rowHeight = UITableView.automaticDimension

    self.globalTableView.sectionHeaderHeight =  UITableView.automaticDimension
    self.globalTableView.estimatedSectionHeaderHeight = 25;

Now we have set row height and section height by using UITableView Delegate methods:

现在我们已经使用 UITableView Delegate 方法设置了行高和节高:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
   {
       return UITableView.automaticDimension
   }
   func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

       return UITableView.automaticDimension

   }

回答by iuriimoz

I tried

我试过

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;

but it didn't size correctly header with multiline label. Added this to solve my problem:

但它没有正确调整带有多行标签的标题。添加了这个来解决我的问题:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Recalculates height
    tableView.beginUpdates()
    tableView.endUpdates()
}

回答by Codus

In my case:

就我而言:

  1. set programmatically not work.
  1. 以编程方式设置不起作用

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension.

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension.

  1. set in storyboard not work.

  2. override heightForHeaderInSectionworked.

  1. 设置在情节提要中不起作用

  2. 覆盖heightForHeaderInSection工作

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}

test enviroment:

测试环境:

  • Mac OS 10.13.4
  • XCode Version 9.4.1
  • Simulator iPhone 8 Plus
  • Mac 操作系统 10.13.4
  • XCode 版本 9.4.1
  • 模拟器 iPhone 8 Plus

回答by arg_vn

Yes, it works for me. I have to make more changes as below:

是的,它对我有用。我必须进行更多更改,如下所示:

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()

    label.numberOfLines = 0
    label.text          = my own text

    return label
}

回答by Irina

I modified iuriimozanswer. Just replaced viewWillAppear method:

我修改了iuriimoz答案。刚刚替换了 viewWillAppear 方法:

tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 25


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Recalculates height
    tableView.layoutIfNeeded() 
}

Also add the tableView.layoutIfNeeded() to

还将 tableView.layoutIfNeeded() 添加到

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)
    tableView.layoutIfNeeded()
}

For iOS 10

对于 iOS 10

tableView.beginUpdates()
tableView.endUpdates()

have "fade" animation effect on viewWillAppear for me

对我来说在 viewWillAppear 上有“淡入淡出”动画效果