[英]removeFromSuperview() suddenly no longer working
I have a UIView
subclass loaded from a xib like so: 我有从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:"))
}
Inside this view, I have a view called blurView
that I'm adding behind a UITextView
. 在此视图内,我有一个称为
blurView
视图,该视图添加在UITextView
后面。 It's added to the view like so: 像这样将其添加到视图中:
self.view.insertSubview(blurView, belowSubview:textOverlay)
When I use the UITapGestureRecognizer
on the blurView
, the blurView
should be removed using this code: 当我在
UITapGestureRecognizer
上使用blurView
,应该使用以下代码删除blurView
:
func tappedOnBlur(tapGesture: UITapGestureRecognizer?) {
blurView.removeFromSuperview()
}
Originally, I was adding this subclass as a subview to a view and it worked perfectly. 最初,我将这个子类作为一个子视图添加到视图中,并且它工作得很好。 However, I am now using a
UIViewController
with its view
set to the subclass so I can push it into view using a UINavigationController
. 但是,我现在使用的
UIViewController
的view
设置为子类,因此可以使用UINavigationController
将其推入视图。 Now that I've done this, if I try to call tappedOnBlur:
, it doesn't remove it from the view. 现在,如果我尝试调用
tappedOnBlur:
它不会将其从视图中删除。 Strangely, if I use blurView.isDescendantOfView(self.view)
it returns false (both before I try to remove it and after). 奇怪的是,如果我使用
blurView.isDescendantOfView(self.view)
它将返回false(在我尝试将其删除之前和之后)。
Does anyone know what's going on here? 有人知道这是怎么回事吗? Thanks.
谢谢。
Just found out it's the way blurView is being loaded. 刚刚发现这是blurView的加载方式。 I have moved the code in
layoutSubviews()
setting up blurView and it's working fine. 我已经在
layoutSubviews()
移动了代码,设置了blurView,它工作正常。 I thought I could use layoutSubviews()
like viewDidLoad()
in a UIViewController
but obviously not 我以为我可以在
UIViewController
使用诸如viewDidLoad()
类的layoutSubviews()
viewDidLoad()
,但显然不能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.