简体   繁体   中英

Using UIScrollView with UIVisualEffectView (UIBlurEffect) in Swift

I'm using UIBlurEffect from UIVisualEffectView to produce the look introduced in iOS 7. To do so I programmatically add anything I want portrayed on top of the blur to the contentView , as described in the UIBlurEffect Class Reference:

A UIBlurEffect object applies a blurring effect to the content layered behind a UIVisualEffectView . Views added to the contentView of a UIVisualEffectView are not affected by the blur effect.

But now I need to add a UIScrollView into the mix, and the fact that I had to use the contentView has left me completely confused. I'm not very familiar with the UIScrollView , but the iOS 8 Swift Programming Cookbook gives this example:

在此处输入图片说明

I'm not in need of displaying an imageView , but instead a separate view with many views inside of it in the hierarchy. Where exactly do I add the scrollView ? Do I set scrollView as a subview of contentView ? Or the other way around, the contentView as a subview of scrollView ? Here's my current code:

class InfoVC: UIViewController {


    @IBOutlet weak var contentView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // blur
        let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
        blur.frame = view.frame
        view.addSubview(blur)
        blur.contentView.addSubview(self.contentView)

    }

You want to scroll something on top of the blur effect, correct?

In that case you need to add the scroll view to the content view of the blur view. Assuming the contentView is the view you want to scroll it should work like this:

let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.light)) 
blur.frame = view.bounds
view.addSubview(blur)
UIScrollView scrollView = UIScrollView(frame:blur.bounds)
blur.contentView.addSubview(scrollView)
scrollView.addSubview(contentView)
scrollView.contentSize = contentView.frame.size // or whatever other size you like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM