RemoveFromSuperview does not work for UIView added to SKScene

I am adding a UIView to the SKScene view. Later, when I want to remove this UIView from my supervisor, using the standard uiview.removeFromSuperview method does not work. How should I do this instead? This is how I add a UIView:

func addContainerView() { let containerRect = CGRectMake(400, 24, 600, 720) smallerView = UIView(frame: containerRect) smallerView.backgroundColor = UIColor.redColor() self.view.addSubview(smallerView) } 

This is how I try to remove it:

 func removeContainerView() { smallerView.removeFromSuperview() } 

All this happens in the SKScene class, so here the "I" refers to this scene. Any thoughts?

+5
source share
1 answer

First of all, I wonder what version of quick use is used.

self.view is optional in 1.2 so you should enter: self.view?.addSubview() if you are targeting swift 1.2

I tried in swift 1.2 to make a simple application

 class GameScene: SKScene { let subview = UIView() override func didMoveToView(view: SKView) { subview.frame = CGRectMake(0, 0, 100, 100) subview.backgroundColor = SKColor.orangeColor() self.view?.addSubview(subview) } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { removeContainerView() } func removeContainerView() { subview.removeFromSuperview() } } 

The above code works very well. I can think of several reasons why your gaze is not removed.

  • Are you sure that removeContainerView indeed called. Try making a breakpoint to see if it's called
  • If you configured your SKView in code, something might be wrong.
  • Your subtask is freed or something happens.

To fully debug your problem, we need to see another code.

We need:

  • Announce your spy
  • All functions calling removeContainerView ()

Even better would be pastebin your SKScene class.

+1
source

Source: https://habr.com/ru/post/1201946/


All Articles