我希望我的表格视图部分标题文本以鲜明的风格呈现,底层的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
}
答案 0 :(得分: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