UIBlurEffect 和 UIVibrancyEffect 图像不显示在视图控制器的视图上

UIBlurEffect and UIVibrancyEffect image doesn't show on a view controller's view

我在一本学习 iOS13 编程的书上看到了一个例子。我将程序片段放在视图控制器的视图中,但图像(在模糊背景上具有活力效果的标签)没有显示在控制器的视图中。

预期图像如下:

程序代码如下:

import UIKit
import CoreImage.CIFilterBuiltins

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        drawBlurAndVibrancyImage()

    }

    func drawBlurAndVibrancyImage () {
        let blurEffect = UIBlurEffect(style: .systemThinMaterial)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.view.addSubview(blurView)
        let vibEffect = UIVibrancyEffect(blurEffect: blurEffect, style: .label)
        let vibView = UIVisualEffectView(effect: vibEffect)
        let lab = UILabel()
        lab.text = "Hello, world!"
        lab.sizeToFit()
        vibView.bounds = lab.bounds
        vibView.center = self.view.bounds.center
        vibView.autoresizingMask = [.flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
        blurView.contentView.addSubview(vibView)
        vibView.contentView.addSubview(lab)
    }
}

非常感谢!

您最初没有为 blurView 的框架提供任何价值。

let blurView = UIVisualEffectView(effect: blurEffect)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurView)

您可以添加 blurView.frame = self.view.bounds,您将开始看到标签和视觉效果视图。