【问题标题】:How to change the height of tableHeaderView in storyboard?如何更改情节提要中 tableHeaderView 的高度?
【发布时间】:2016-08-05 10:30:40
【问题描述】:

我试图在情节提要中自定义 tableHeaderView,问题是 tableHeaderView 似乎有固定大小。当我运行这个程序时,它看起来像这样: my custom headerView

其实两个灰色的UILabel是我headerView的一部分,但是你可以看到,它有固定的大小。而且我已经尝试在 viewDidLoad 中更改 headerView 的大小:

    UIView *headerView = self.tableView.tableHeaderView;
    CGRect frame = headerView.frame;
    frame.size = CGSizeMake(frame.size.width, frame.size.width / 2.6);
    headerView.frame = frame;
    [self.tableView updateConstraintsIfNeeded];

P.S:我见过一些类似的问题,但他们只是用 updateConstraintsIfNeeded 解决了它,但我不知道它对我不起作用。我的 Xcode 版本是 7.2,谢谢!

【问题讨论】:

  • 添加高度约束并为其分配动态值。
  • 你试过了吗 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  • @SaadChaudhry 好吧,我试过使用 heightForHeaderInSection,有趣的是,它只是在我的自定义 headerView 下添加了另一个标题。
  • 是的,因为它不是 tableview 的标题而是部分标题,您是否将自定义标题分配给 tableview? self.tableView.tableHeaderView = headerView;
  • @SaadChaudhry 我尝试添加一个约束,但它似乎仍然不适合我。也许来自故事板的视图被分配了一个静态框架,即使我可以改变它的真实框架,但从 tableView 它只是保持原来的大小。

标签: ios objective-c uitableview storyboard


【解决方案1】:

试试这个代码

headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

【讨论】:

    【解决方案2】:

    只要按照代码:

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    labelView.text = @"Some Text";
    
    [headerView addSubview:labelView];
    self.tableView.tableHeaderView = headerView;
    

    【讨论】:

    • 但是这样我只是得到了一个空白,我已经在storyboard中做了我的自定义headerview,我只需要改变它的高度。
    • 空白区域是您在标题中添加的 UIView。您需要添加一个标签以在标题中提供一些文本。
    • 我明白了,这样我只是写了一些关于我的自定义 headerView 的代码。事实上,我想弄清楚如何用一点额外的代码来完成故事板中的所有过程。
    【解决方案3】:

    为什么不能创建一个自定义的UIView 子类并在其中添加标签?

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100;//whatever size you want
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionInfo = fetchedResultsController!.sections! as [NSFetchedResultsSectionInfo]
    
        let headerView = CustomView()
        headerView.backgroundColor = UIColor.redColor()
        headerView.addLabels()//add two labels in this method
        return headerView
    }
    

    【讨论】:

    • 好吧,我只是想尝试在storyboard中自定义headerView。
    • @WeiDu:然后创建一个并在其中添加带有约束的标签,提供您想要的任何高度!
    【解决方案4】:

    您在使用 AutoLayout 吗?请关注这个

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

    【讨论】:

      猜你喜欢
      • 2011-01-02
      • 2019-01-08
      • 2015-03-08
      • 1970-01-01
      • 2013-10-26
      • 2012-02-09
      • 2017-10-18
      • 2018-12-05
      • 2019-10-15
      相关资源
      最近更新 更多