iOS 8 中的 UITableViewHeaderFooterView

标签 uitableview fonts ios8 textcolor

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

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 的展示方式:
enter image description here

以下是 iOS 8 的展示方式:
enter image description here
setFont: 在这里似乎没有生效,或者 15pt 字体在 iOS 8 上比在 iOS 7 上大

这是我删除 setFont 时 iOS 8 的显示方式:调用
enter image description here

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

我是否遗漏了什么或那些是“测试版错误”(我使用的是来自 XCode6 GM 种子的模拟器,我在装有 iOS 8 beta 5 的 iPhone 5 上遇到了同样的问题)?

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

最佳答案

[SWIFT 版本] 刚刚在 UITableView UITableViewStylePlain 中遇到了同样的问题,即 Header 字体设置在

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

标题高度需要“手动”调整:
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;

}

其余的是标准的,但为了完整起见,这里显示:
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!;
      }

关于iOS 8 中的 UITableViewHeaderFooterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888060/

相关文章:

ios - 如何使 uiscrollview 仅适用于 ios 的垂直滚动?

ios - 如果我为表格 View 单元格设置动画,表格 View 滚动不会停止

快速添加可点击的表格 View

ios - 在 UITableView 中交换项目 - 有 UI 问题

objective-c - UITableViewCell 中的 UISegmentedControl 在通过 dequeueReusableCellWithIdentifier 重用时更改段索引顺序

css - 如何在 CSS 文件中调用 Google API 字体?

css - 一些 unicode 不显示

Python3 Tkinter 字体不工作

ios - 与 AVFoundation 有点混淆

ios - 我们可以在iOS中部分隐藏UIView吗