1

我希望我的表格视图部分标题文本以充满活力的风格呈现,其中底层 UI 通过文本闪耀。我尝试了以下方法,但标题中没有出现任何内容,好像我返回了nil.

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //Add vibrancy text effect
    let blurEffect = UIBlurEffect(style: .Light)
    let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
    let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)

    //Label for vibrant text
    let vibrantLabel = UILabel()
    vibrantLabel.text = "testing"
    vibrantLabel.font = UIFont.systemFontOfSize(11.0)
    vibrantLabel.textColor = UIColor(white: 0.64, alpha: 1)
    vibrancyEffectView.contentView.addSubview(vibrantLabel)

    return vibrancyEffectView
}
4

1 回答 1

3

解决了。问题中提供的代码的唯一问题是:我必须为视图和标签设置框架(duh),并且我需要指定模糊效果样式以确保文本是浅色而不是深色.Dark.Light对于任何有同样问题的人,这里是代码。我确定你会想要调整文本的位置/大小/等。请注意,我已经为表格视图的背景应用了模糊效果。

//Create vibrancy text effect
let blurEffect = UIBlurEffect(style: .dark)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 40)
vibrancyEffectView.autoresizingMask = .flexibleWidth

//Create header label
let vibrantLabel = UILabel()
vibrantLabel.frame = vibrancyEffectView.frame
vibrantLabel.autoresizingMask = .flexibleWidth
vibrantLabel.text = "testing"
vibrantLabel.font = UIFont.systemFont(ofSize: 16)
vibrantLabel.textColor = UIColor(white: 0.64, alpha: 1)

vibrancyEffectView.contentView.addSubview(vibrantLabel)
return vibrancyEffectView

最终效果图

于 2014-10-12T02:41:08.467 回答