在 UITableViewHeaderFooterView 中从 tableView 垂直居中 textLabel: willDisplayHeaderView: forSection: [英] Centering textLabel vertically in UITableViewHeaderFooterView from tableView: willDisplayHeaderView: forSection:

查看:26
本文介绍了在 UITableViewHeaderFooterView 中从 tableView 垂直居中 textLabel: willDisplayHeaderView: forSection:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 tableViewHeader 的文本标签垂直居中.这是来自 - (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section:

I am trying to vertically center the text label for a tableViewHeader. Here is the relevant code from - (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section:

- (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    if(![view isKindOfClass:[UITableViewHeaderFooterView class]]){
        return;
    }

    UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view;
    UILabel* label = tableViewHeaderFooterView.textLabel;

    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont rev_lightAppFontOfSize: 24.0f];
    label.textColor = [UIColor rev_colorWithHex: 0x858585];
    label.text = [label.text capitalizedString];

    label.center = tableViewHeaderFooterView.center; // label not in superview yet

    tableViewHeaderFooterView.layer.borderColor = [UIColor greenColor].CGColor;
    tableViewHeaderFooterView.layer.borderWidth = 2.0f;

    tableViewHeaderFooterView.contentView.layer.borderColor = [UIColor purpleColor].CGColor;
    tableViewHeaderFooterView.contentView.layer.borderWidth = 2.0f;

    label.layer.borderColor = [UIColor orangeColor].CGColor;
    label.layer.borderWidth = 2.0f;

}

标签对象不是 tableViewHeaderFooterView 的子视图,因此居中代码似乎不起作用.有什么想法吗?

the label object is not a subview of tableViewHeaderFooterView so the centering code does not seem to be working. Any thoughts?

推荐答案

好吧,我最终想出了如何做到这一点.不幸的是,您无法在 tableView:willDisplayHeader: 函数中处理垂直居中,因为 tableViewHeaderFooterView 在幕后做了一些棘手的 tableView.因此我发现最好的方法是在您自己的标题视图中返回一个标签.相关代码在这里:

Well I ended up figuring out how to do this. Unfortunately you cannot handle vertical centering in the tableView:willDisplayHeader: function because the tableViewHeaderFooterView does some tricky tableView behind the scenes stuff. Therefore I found the best way is to return a label in your own header view. The relevent code is here:

- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger)  section
{
    UIView* view = [[UIView alloc] init];
    UILabel* label = [[UILabel alloc] init];

    label.text = [self tableView: tableView titleForHeaderInSection: section];
    label.textAlignment = NSTextAlignmentCenter;

    [label sizeToFit];
    label.translatesAutoresizingMaskIntoConstraints = NO;

    [view addSubview:label];

    [view addConstraints:
     @[[NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterX
                                 multiplier:1 constant:0],
     [NSLayoutConstraint constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterY
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:view
                                  attribute:NSLayoutAttributeCenterY
                                 multiplier:1 constant:0]]];

    return view;
}

希望这能帮助其他人!

干杯

这篇关于在 UITableViewHeaderFooterView 中从 tableView 垂直居中 textLabel: willDisplayHeaderView: forSection:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆