我从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(在我尝试删除它之前和之后)。
有谁知道这里发生了什么? 感谢。
答案 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()
,但显然不是