简体   繁体   English

自动布局的tableHeaderView无法正常工作

[英]tableHeaderView with Autolayout not working correctly

Hey everybody i have a TableHeaderView and everything gets managed by Autolayout: 大家好,我有一个TableHeaderView,一切都由Autolayout管理:

  • The UIImageView at the top should be always 2:1, so i set the Aspect ration and the Rest of the needed Constraints. 顶部的UIImageView应该始终为2:1,因此我设置了纵横比和其余所需的约束。

  • The 4 UIButtons should be always horizontal and should have the same Height and Width. 4个UIButtons应始终为水平,并且高度和宽度应相同。 So i worked with Equal Width and Equal Height and also with a Aspect Ratio of 1:1 所以我使用等宽和等高的方法,并且纵横比为1:1

  • And i have two UILabels with numberOfLines set to 0. I also made a Subclass of my UILabel , because of the preferredMaxLayoutWidth , like this: 和我有两个UILabelsnumberOfLines设置为0,我也把我的子类UILabel ,因为的preferredMaxLayoutWidth ,就像这样:

     - (void) layoutSubviews { [super layoutSubviews]; if ( self.numberOfLines == 0 ) { if ( self.preferredMaxLayoutWidth != self.frame.size.width ) { self.preferredMaxLayoutWidth = self.frame.size.width; [self setNeedsUpdateConstraints]; } } } 

This is my Code: 这是我的代码:

- (void)initializeHeaderViewLabels
{
 //After the Response from my server arrived i set the tableHeaderView with the Textdata
self.tableView.tableHeaderView = nil;

if((self.contentDict[@"title"]) != [NSNull null])
{
    self.headerTitleLabel.text = (self.contentDict[@"title"]);
}

if((self.contentDict[@"shortDescription"]) != [NSNull null])
{
    self.headerDescriptionLabel.text = (self.contentDict[@"shortDescription"]);
}

[self.headerView setNeedsLayout];
[self.headerView layoutIfNeeded];

CGFloat height = [self.headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

CGRect headerFrame = self.headerView.frame;
headerFrame.size.height = height;
self.headerView.frame = headerFrame;

[self.tableView setTableHeaderView:self.headerView];

}

I get my Image and the Text for the UILabels from my Server, so i have to wait until the Response arrives, then i call initializeHeaderViewLabels . 我从服务器上获取了UILabels图像和文本,因此必须等到Response到达后,再调用initializeHeaderViewLabels

**My Problem is that the tableHeaderView is way too large during Runtime and so my UILabels get stretched and there is a lot of whiteSpace. **我的问题是,在运行时tableHeaderView太大了,因此我的UILabels被拉伸了,并且有很多空白。 Maybe i miss something? 也许我想念什么?

In order to make it work you'll need some magic or migrate away from table header view . 为了使它起作用,您需要一些魔术或从表头视图迁移出去 I've already answered on a similar question here . 我已经在这里回答了类似的问题。 The trick is to magically reset tableHeaderView after evaluating it's height via autolayout. 诀窍是通过自动布局评估其高度后,神奇地重置tableHeaderView I've created sample project for that: TableHeaderView+Autolayout . 我已经为此创建了示例项目: TableHeaderView + Autolayout

Try below solution 尝试以下解决方案

- (void)sizeHeaderToFit
{
    UIView *header = self.tableView.tableHeaderView;

    [header setNeedsLayout];
    [header layoutIfNeeded];

    CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = header.frame;

    frame.size.height = height;
    header.frame = frame;

    self.tableView.tableHeaderView = header;
}

Source: 资源:

table header view height is wrong when using auto layout, IB, and font sizes 使用自动布局,IB和字体大小时,表格标题视图高度错误

Also refer below question...! 另请参阅以下问题...!

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

My problem was that I was setting constraints on the my header view that were mapping some frame values to superview frame values. 我的问题是我在标头视图上设置了约束,这些约束将一些框架值映射到超级视图框架值。 When I removed the constraints and just set the frame directly, everything worked. 当我删除约束并直接设置框架时,一切正常。

Header view height is set in the table view delegate. 标题视图高度是在表视图委托中设置的。 By default it is the same size as in interface builder which is too large when run in app since the width for interface builder is 600. 默认情况下,它与界面构建器中的大小相同,在应用程序中运行时太大,因为界面构建器的宽度为600。

So what you need is to implement this method in UITableViewDelegate 因此,您需要在UITableViewDelegate实现此方法

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 200.0 // Set the desired height based on your device or what ever you are using.
}

And Objective-C version: 和Objective-C版本:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    return 200;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM