2016-08-05 38 views
0

我试图自定义故事板中的tableHeaderView,问题是,似乎tableHeaderView具有固定的大小。当我运行这个程序时,它看起来像这样: my custom headerView如何更改故事板中tableHeaderView的高度?

实际上,两个灰色的UILabel是我的headerView的一部分,但正如你所看到的,它有固定的大小。我已经尝试过改变我headerView的大小viewDidLoad中:

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

PS:我已经看到了一些类似的问题,但他们只是updateConstraintsIfNeeded解决了这个问题,但我不知道它不” t适合我。我的Xcode版本是7.2,谢谢!

+0

添加约束高度和分配给它的动态值。 –

+0

你试过 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; –

+0

@SaadChaudhry嗯,我试过使用heightForHeaderInSection,有趣的是,它只是在我的自定义headerView下添加另一个头。 –

回答

0

只需按照代码:

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; 
+0

但是这样我得到了一个空白区域,我已经在故事板中制作了自定义标题,我只需要改变它的高度。 –

+0

白色空间是您在标题中添加的UIView。您需要添加一个标签以在标题中提供一些文本。 –

+0

我更新了答案。只要按照它 –

0

你用自动版式的工作?请点击此

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

看来这不适合我。 –

0

你为什么不能创建一个自定义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 
} 
+0

嗯,我只是想尝试在故事板中制作自定义headerView。 –

+0

@WeiDu:然后创建一个并添加带有约束的标签,提供您想要的任何高度! – D4ttatraya

1

试试这个代码

headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
相关问题