UITableView上的模糊和鲜艳效果

3
我不知道如何实现这个。我已经尝试了Stackoverflow上的所有方法,但是都没有生效。我有一个视图,在其下面有一张图片和一个tableView。但是tableView仍然是白色的。是否有人知道如何做到这一点?正如我所写的,从这里获取的方法都没用。
我想要这种UITableView:
导航栏也是我的问题之一(模糊和活力)。
在使用以下代码后:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    let backgroundImageView = UIImageView(image: UIImage(named: "background3.png"))

    cell.backgroundView = backgroundImageView

    var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView

    visualEffectView.frame = CGRectMake(0, 0, cell.bounds.width, cell.bounds.height)

    backgroundImageView.addSubview(visualEffectView)

    cell.textLabel?.text = self.field[indexPath.row]
    cell.textLabel?.textColor = UIColor.whiteColor()
    cell.textLabel?.backgroundColor = UIColor.clearColor()

    return cell
}

我的tableView现在看起来像这样:

模拟器截图

这很合理,因为这是background3.png图像的外观。


3
我已经厌倦了那些匿名下投票却不给出理由的懦夫们。这个问题没有任何问题。 - μολὼν.λαβέ
2个回答

9

继承UITableViewController。

let effect = UIBlurEffect(style: .Dark)
let resizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

构建一个像这样的背景视图并将其粘贴到您的表视图中。请注意分隔符效果,这是新的API。
override func viewDidLoad() {
    super.viewDidLoad()
    
    let backgroundView = UIView(frame: view.bounds)
    backgroundView.autoresizingMask = resizingMask
    backgroundView.addSubview(self.buildImageView())
    backgroundView.addSubview(self.buildBlurView())

    tableView.backgroundView = backgroundView
    tableView.separatorEffect = UIVibrancyEffect(forBlurEffect: effect)
}

func buildImageView() -> UIImageView {
    let imageView = UIImageView(image: UIImage(named: "coolimage"))
    imageView.frame = view.bounds
    imageView.autoresizingMask = resizingMask
    return imageView
}

func buildBlurView() -> UIVisualEffectView {
    let blurView = UIVisualEffectView(effect: effect)
    blurView.frame = view.bounds
    blurView.autoresizingMask = resizingMask
    return blurView
}

将单元格的背景色更改为透明,这样当您有单元格时,背景就会显示出来。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as! UITableViewCell
    cell.backgroundColor = UIColor.clearColor()
    return cell
}

1
是的,那就是Stackoverflow。我找到了自己问题的解决方案,但有人给我点了踩 :-D - Kotora
这个可以工作。但是你必须更新resizingMask的代码以适用于Swift 2.0,我点赞了,谢谢 :) - Tolgay Toklar
不好意思,你理解错了。我建议修改这个答案,我已经为Swift 2.0进行了修复,你当前的代码在Swift 2.0上会返回编译器错误。 - Tolgay Toklar
Swift 2.0 语法:let resizingMask: UIViewAutoresizing = [.FlexibleWidth, .FlexibleHeight] - Rygen

-3
所以解决方案非常简单。我在Youtube上找到了一个很棒的教程:

Swift iOS教程 - 在Xcode 6中自定义表格外观

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

    var openSansSemibold = UIFont(name: "OpenSans-Semibold", size: 12)?.fontDescriptor().fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits.TraitBold)
    cell.textLabel?.font = UIFont(descriptor: openSansSemibold!, size: 12)

    if(indexPath.row % 2 == 1){
        cell.backgroundColor = UIColor.clearColor()
    }else{
        cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
        cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
        cell.detailTextLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
    }

    cell.textLabel?.textColor = UIColor.whiteColor()

    return cell
}

在viewDidLoad()方法中:
self.tableView.rowHeight = 70
self.tableView.backgroundView = UIImageView(image: UIImage(named: "background"))

就是这样。每个奇数单元格都有清晰的颜色,每个偶数单元格都有模糊的背景。看起来非常棒!


你只是在设置alpha值...这里根本没有应用任何模糊效果。请参考我的答案,了解如何使用UIVibrancyEffect类,它是最重要的部分。 - user1951992
此外,您设置了每个其他单元格,这不是 OP 所要求的。 - user1951992
同时,您正在使用字体描述符来提取已经是半粗体的半粗体字体... - user1951992
此外,大部分代码并非必要的,只是为了支持你的视图层次结构而已,并不适用于 OP 的情况。 - user1951992

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接