2015-05-30 148 views
0

我有一個UIView子從廈門國際銀行加載像這樣:removeFromSuperview()突然不再工作

// The xib custom view 
private var view: UIView! 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    xibSetup() 
} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    xibSetup() 
} 

/** 
* Setting up the view 
*/ 
private func xibSetup() { 
    view = loadViewFromNib() 

    // use bounds not frame or it'll be offset 
    view.frame = bounds 

    // Make the view stretch with containing view 
    view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight 

    // Adding custom subview on top of our view (over any custom drawing > see note below) 
    addSubview(view) 

} 

/** 
* Loading the custom view from a nib 
*/ 
private func loadViewFromNib() -> UIView { 
    // Load the nib 
    let bundle = NSBundle(forClass: self.dynamicType) 
    let nib = UINib(nibName: "EditImageView", bundle: bundle) 

    // Assumes UIView is top level and only object in PapControls.xib file 
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 
    return view 
} 

override func layoutSubviews() { 
    super.layoutSubviews() 
    blurView = UIVisualEffectView(effect: UIBlurEffect(style: .Dark)) 
    blurView.frame = self.bounds 
    blurView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tappedOnBlur:")) 
} 

裏面這個觀點,我有一個名爲blurView認爲我加入一個UITextView後面。它添加到視圖,像這樣:

self.view.insertSubview(blurView, belowSubview:textOverlay) 

當我在blurView使用UITapGestureRecognizer,該blurView應使用此代碼刪除:

func tappedOnBlur(tapGesture: UITapGestureRecognizer?) { 
    blurView.removeFromSuperview() 
} 

本來,我加入這個子類作爲一個子視圖以一個看法,它完美的工作。但是,我現在使用UIViewController及其view設置爲子類,因此我可以使用UINavigationController將它推入視圖。現在我已經完成了這項工作,如果我嘗試撥打tappedOnBlur:,它不會從視圖中刪除它。奇怪的是,如果我使用blurView.isDescendantOfView(self.view),它會返回false(在我嘗試刪除它之前和之後)。

有人知道這裏發生了什麼嗎? 謝謝。

回答

1

剛發現這是blurView加載的方式。我已經移動layoutSubviews()中的代碼設置blurView,它工作正常。我想我可以使用layoutSubviews(),如viewDidLoad()UIViewController,但顯然不是