简体   繁体   中英

Add uiview to be the background view

I have the following blur view, that I would like to be the background view. The problem is that it becomes the first view, hiding underneath labels/cells etc. I tought that self.sendSubview(toBack: blurEffectView) would do just that - do you know why? :

Update: also tried: self.insertSubview(blurEffectView, belowSubview: self)

import Foundation
import UIKit

extension UIView
{
    func addBlurEffect()
    {
        if !UIAccessibilityIsReduceTransparencyEnabled() {

            self.backgroundColor = UIColor.clear
            let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = self.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.addSubview(blurEffectView)
            self.sendSubview(toBack: blurEffectView)
        } else {
            self.backgroundColor = UIColor.white
        }

    }
}

It turns out that it is only when using UIViewController or UItableView.

import Foundation
import UIKit

extension UIView
{
    func addBlurEffect()
    {
        if !UIAccessibilityIsReduceTransparencyEnabled() {

            self.backgroundColor = UIColor.clear
            let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            blurEffectView.frame = self.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            if (self is UICollectionView){
                (self as! UICollectionView).backgroundView = blurEffectView
            }else{
                self.addSubview(blurEffectView)
                self.insertSubview(blurEffectView, belowSubview: self)
            }

        } else {
            self.backgroundColor = UIColor.white
        }

    }
}

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