2017-06-20 103 views
0

我有以下看法模糊,我想爲背景圖。問題是,它成爲第一個視圖,隱藏在標籤/單元格下面等我想self.sendSubview(toBack: blurEffectView)會做到這一點 - 你知道爲什麼嗎? :添加的UIView爲背景視圖

更新:也試過: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 
     } 

    } 
} 
+1

哪裏是在視圖層次對此有何看法?你有沒有將blurView添加到VC的'self.view'?如果是這樣,你需要在VC上調用'self.view.sendSubview(toBack:myBlurView)'。 – vacawama

+0

謝謝。事實證明,只有在使用UIViewController或UItableView時纔是如此。 –

回答

0

事實證明,它使用的UIViewController或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 
     } 

    } 
}