【问题标题】:tableHeaderView with Autolayout not working correctly具有自动布局的 tableHeaderView 无法正常工作
【发布时间】:2015-07-06 06:12:55
【问题描述】:

大家好,我有一个 TableHeaderView,一切都由 Autolayout 管理:

  • 顶部的UIImageView 应始终为 2:1,因此我设置了纵横比和其他所需的约束。

  • 4 UIButtons 应该始终是水平的,并且应该具有相同的高度和宽度。所以我使用了等宽和等高以及 1:1 的纵横比

  • 我有两个UILabelsnumberOfLines 设置为 0。由于preferredMaxLayoutWidth,我还创建了我的UILabel 的子类,如下所示:

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

这是我的代码:

- (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];

}

我从我的服务器获取UILabels 的图像和文本,所以我必须等到响应到达,然后我调用initializeHeaderViewLabels

**我的问题是 tableHeaderView 在运行时太大了,所以我的 UILabels 被拉伸并且有很多空白。也许我错过了什么?

【问题讨论】:

  • 能否发一张运行时结果的截图?
  • 和headerView,有什么限制,在哪里初始化?
  • 我添加了另一个截图以便更好地理解!
  • 你有从description 到视图底部的约束吗?
  • 是的,底部有约束

标签: ios objective-c uitableview nstableheaderview


【解决方案1】:

标题视图高度在表视图委托中设置。默认情况下,它与界面构建器中的大小相同,在应用程序中运行时太大了,因为界面构建器的宽度为 600。

所以你需要在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.
}

和 Objective-C 版本:

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

【讨论】:

  • 澄清一下,自动布局无法确定表头的高度。
  • 我说的是tableHeaderView!不是 headerSection,这些是完全不同的东西!
  • 你试过在headerView上调用sizeToFit吗?也许您需要先为标签调用它。
  • @Neal 你说得很对,但是错了,有一些魔法是可能的。
  • @MANIAK_dobrii 你是对的,但为简单起见,对于初学者来说,“大部分是真实的”是你能提供的最好的帮助。
【解决方案2】:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
    return 200;
}

【讨论】:

  • 这是为 headerInSection 而不是 tableHeaderView!
【解决方案3】:

为了使其工作,您需要一些魔法或从表格标题视图迁移。我已经回答了类似的问题here。诀窍是在通过自动布局评估其高度后神奇地重置tableHeaderView。我为此创建了示例项目:TableHeaderView+Autolayout

【讨论】:

  • 首先,遗憾的是,Apple 无法以“正常”方式使用自动布局。第二:谢谢你它真的很完美!当然,我会尽快奖励我的赏金!
【解决方案4】:

试试下面的解决方案

- (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;
}

来源:

table header view height is wrong when using auto layout, IB, and font sizes

另请参阅下面的问题...!

How do I set the height of tableHeaderView (UITableView) with autolayout?

【讨论】:

    【解决方案5】:

    我的问题是我在标题视图上设置了约束,这些约束将一些帧值映射到超级视图帧值。当我删除约束并直接设置框架时,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 2015-04-23
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多