0

In my UINavigationController, I add an UIView by code. I want to remove this UIView when my View disappear but I can't find the right way to do that. Here's the I'm using:

var patch: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    patch = UIView(frame: CGRectMake(0, 0, view.bounds.width, 20))
    patch.backgroundColor = UIColor.redColor()
    self.view.addSubview(patch)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)

    patch.removeFromSuperview()
    // or
    self.view.willRemoveSubview(patch)
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        patch.hidden = true
    } else {
        patch.hidden = false
    }
}

and nothing, view is still there.

I tried even setting patch to nil or to CGRectMake(0, 0, 0, 0) but nothing...

What's the right code to delete it?

Matte.Car
  • 2,007
  • 4
  • 23
  • 41
  • Why do you need to remove it when the parent view is already disappearing? If you only add it in `viewDidLoad` then there is nothing more to do, surely? – l00phole Oct 18 '15 at 09:26
  • This UIView should be visible when device is Portrait and hidden in Landscape. So if I close view then I come back there, code generates another UIView and new UIView is managed by code so disappear when screen rotate, but the older is always on top. – Matte.Car Oct 18 '15 at 09:29
  • I truly believe, you are not managing your views well. You should create only one `UIView` instance - make it a global variable. If you want to hide or show your view then use `hidden` property on it for portrait or landscape mode. – Abhinav Oct 18 '15 at 09:40
  • I'm managing UIView in this way http://stackoverflow.com/questions/33187856/tableview-scroll-underlying-top-bar/33191297#33191297 , it should be right... – Matte.Car Oct 18 '15 at 09:41
  • ok, this looks good. Can you please ensure that you are calling `removeFromSuperview()` on correct object. The object that is drawn on UI. This is the correct method to remove a `UIView` from view hierarchy. – Abhinav Oct 18 '15 at 09:46
  • I updated question with complete code I'm using. NOTE: this code is in a UINavigationController! – Matte.Car Oct 18 '15 at 09:50
  • Ok, you are removing your view in `viewWillDisappear` and this might be getting called when you go out of current view controller. So, how do you know if view is not removed because then screen will be loaded with new view, right? – Abhinav Oct 18 '15 at 09:53
  • Using breakpoints I saw code is running. However I used even `viewWillUnload` and `viewDidUnload`, but it's the same. – Matte.Car Oct 18 '15 at 09:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92636/discussion-between-abhinav-and-matte-car). – Abhinav Oct 18 '15 at 09:57
  • `patch.removeFromSuperview()` before `super.viewWillDisappear(animated)`. – Cœur Oct 18 '15 at 11:14

0 Answers0