首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewHeaderFooterView in iOS 8

UITableViewHeaderFooterView in iOS 8
EN

Stack Overflow用户
提问于 2014-09-17 10:22:30
回答 4查看 4.9K关注 0票数 8

我有一个UITableViewHeaderFooterView,其中我更改了textLabel字体和背景色。

代码语言:javascript
复制
UITableViewHeaderFooterView* header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
if(!header)
{
    header = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"header"];
    [header.textLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [header.contentView setBackgroundColor:[UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1]];
}

下面是iOS 7如何展示它:

下面是iOS 8如何展示它:

setFont:似乎不会在这里生效,或者iOS 8上的15pt字体比iOS 7上的字体大

下面是iOS 8在删除setFont:调用时如何显示它的方法

如您所见,setFont对字体没有影响,但对textColor有影响。

我是不是遗漏了一些东西,或者那些是"beta bug“(我使用的是XCode6 GM种子的模拟器,iPhone 5和iOS 8 beta 5有相同的问题)?

编辑: iOS 8发行版和XCode 6.0.1似乎没有解决问题

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-09-30 14:30:06

基于tubtub的回答,我创建了一个UITableViewHeaderFooterView子类:

代码语言:javascript
复制
@interface CompatibilityTableViewHeaderFooterView : UITableViewHeaderFooterView
@property (nonatomic,readonly) UILabel* compabilityTextLabel;
@end

@implementation CompatibilityTableViewHeaderFooterView
{
    UILabel* iOS8TextLabel;
    NSLayoutConstraint* iOS8TextLabelLeftMarginConstraint;
}

-(instancetype) initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier])
    {
        self.contentView.backgroundColor = GRIS_213;
        self.compabilityTextLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    return self;
}

-(UILabel*) compabilityTextLabel
{
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0)
    {
        return self.textLabel;
    }
    else
    {
        if (!iOS8TextLabel)
        {
            iOS8TextLabel = [[UILabel alloc] init];
            iOS8TextLabel.translatesAutoresizingMaskIntoConstraints = NO;

            [self addSubview:iOS8TextLabel];

            iOS8TextLabelLeftMarginConstraint = [NSLayoutConstraint constraintWithItem:iOS8TextLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];

            [self addConstraint:iOS8TextLabelLeftMarginConstraint];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[iOS8TextLabel]-(>=0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iOS8TextLabel)]];
            [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[iOS8TextLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(iOS8TextLabel)]];
        }
        return iOS8TextLabel;
    }
}

-(UITableView*) searchForTableView
{
    UIView* currentView = self;
    while (currentView)
    {
        currentView = currentView.superview;
        if ([currentView isKindOfClass:[UITableView class]])
        {
            return (UITableView*)currentView;
        }
    }
    return nil;
}

-(void) layoutSubviews
{
    [super layoutSubviews];

    UITableView* tableView = [self searchForTableView];
    if (tableView)
    {
        iOS8TextLabelLeftMarginConstraint.constant = tableView.separatorInset.left;
    }
}

基本上,我现在只使用compabilityTextLabel属性,而不是textLabel。注意,左空间约束会自动更新,以匹配tableView分隔符内嵌。请随意评论/改进我的代码;)

票数 -1
EN

Stack Overflow用户

发布于 2015-07-17 02:19:09

SWIFT版本在UITableView UITableViewStylePlain中也有同样的问题,即在

代码语言:javascript
复制
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) {...} 

没有效果。下面是我的UITableViewController子类的代码,它在XCode 6.4、iOS 8.4中对我进行了测试,参见http://www.elicere.com/mobile/swift-blog-2-uitableview-section-header-color/

代码语言:javascript
复制
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as? UITableViewHeaderFooterView //recast your view as a UITableViewHeaderFooterView
    if (header == nil) {
      return;
    }
    if (myHeaderFont != nil) {
      header!.textLabel.font = myHeaderFont;
    } 
  }

标题高度需要“手动”调整:

代码语言:javascript
复制
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (myHeaderFont == nil) {
  return  20; //DEFAULT_HEADER_HEIGHT_IN_POINTS;
}
return myHeaderFont.pointSize * 2; //HEIGHT_REL_TO_FONT;

}

其余的都是标准的,但这里显示的是完整的:

代码语言:javascript
复制
override func viewDidLoad() {
//...
//https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewHeaderFooterView_class/index.html#//apple_ref/doc/uid/TP40012241
    self.tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HEADER_REUSE_ID")
 //...
}
      override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var header = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HEADER_REUSE_ID") as? UITableViewHeaderFooterView;
        if (header == nil) {
          header = UITableViewHeaderFooterView(reuseIdentifier: "HEADER_REUSE_ID");
        }
        header!.textLabel.text = myTitle;
        return header!;
      }
票数 2
EN

Stack Overflow用户

发布于 2014-09-19 15:38:10

我也遇到了同样的问题,最后得到了子类UITableViewHeaderFooterView。

下面是一个小的参考实现:只需设置header.headerLabel.text = @"myString";

代码语言:javascript
复制
@interface BHRSectionHeaderView : UITableViewHeaderFooterView

@property (nonatomic, strong) UILabel *headerLabel;

@end



@implementation BHRSectionHeaderView

- (UILabel *)headerLabel
{
    if (!_headerLabel)
    {
        _headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _headerLabel.font = [UIFont boldSystemFontOfSize:13.f];
        _headerLabel.textColor = [UIColor redColor];

        _headerLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:_headerLabel];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(16)-[header]-(>=0)-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:@{ @"header": _headerLabel }]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[header]-(>=0)-|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:@{ @"header": _headerLabel }]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:_headerLabel
                                                     attribute:NSLayoutAttributeCenterY
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeCenterY
                                                    multiplier:1.0f
                                                      constant:0.0f]];
    }

    return _headerLabel;
}

@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25888060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档