removeFromSuperview()突然不再起作用了

时间:2015-05-30 15:43:21

标签: ios swift

我从xib加载了UIView子类,如下所示:

// 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)

当我使用UITapGestureRecognizer上的blurView时,应使用以下代码移除blurView

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

最初,我将这个子类作为子视图添加到视图中,并且它工作得很好。但是,我现在使用UIViewController并将view设置为子类,因此我可以使用UINavigationController将其推送到视图中。既然我已经这样做了,如果我尝试调用tappedOnBlur:,它就不会从视图中删除它。奇怪的是,如果我使用blurView.isDescendantOfView(self.view),它会返回false(在我尝试删除它之前和之后)。

有谁知道这里发生了什么? 感谢。

1 个答案:

答案 0 :(得分:1)

刚刚发现了blurView的加载方式。我已经在use XML::Twig; XML::Twig->new( 'twig_handlers' => { 'Stand' => sub { print "\n" }, 'Attribute' => sub { print $_ -> text,"\t" } } ) ->parsefile ( 'your_file_name.xml' ); 设置了blurView中移动了代码,它运行正常。我想我可以在layoutSubviews()中使用layoutSubviews() viewDidLoad(),但显然不是