user1960169
user1960169

Reputation: 3653

How to remove a shared view in swift

Hello I added a UIView to my ViewController in this way.

self.vWindow = UIApplication.sharedApplication().keyWindow
    let mainBound = CGRectMake(0, self.view.frame.size.height-50, self.view.frame.size.width, 135)//CGSizeMake(UIScreen.mainScreen().bounds.size.width, 135.0)
    let lblVideo = UILabel.init(frame: CGRectMake(10, 0, 100, 50))
    lblVideo.text = "Videos"
    lblVideo.textColor = UIColor.yellowColor()
    self.vwVideo = UIView.init(frame: mainBound)
    self.vwVideo.backgroundColor = UIColor.blackColor()
    vWindow!.addSubview(self.vwVideo)

When I go to particular ViewController from this ViewController I want to remove this bottom view.I did self.vwVideo.removeFromSuperview() in viewDidDissappear() but it doesn't remove my bottom view. How can I do it? Please help me.

Upvotes: 1

Views: 440

Answers (2)

Frank Eno
Frank Eno

Reputation: 2659

You may also declare your label as a global outlet, in a .swift file right below your import statements:

var lblVideo = UILabel()

then you can use lblVideo.removeFromSuperview() in viewDidDisappear()

Upvotes: 0

Alessandro Ornano
Alessandro Ornano

Reputation: 35402

Change your code with:

self.vWindow = UIApplication.sharedApplication().keyWindow
let mainBound = CGRectMake(0, self.view.frame.size.height-50, self.view.frame.size.width,135)
//CGSizeMake(UIScreen.mainScreen().bounds.size.width, 135.0)
let lblVideo = UILabel.init(frame: CGRectMake(10, 0, 100, 50))
lblVideo.text = "Videos"
lblVideo.textColor = UIColor.yellowColor()
self.vwVideo = UIView.init(frame: mainBound)
self.vwVideo.tag = 666 // You must tag your video to find after and to destroy it
self.vwVideo.backgroundColor = UIColor.blackColor()
vWindow!.addSubview(self.vwVideo)

After, when you want to destroy your view it's enough to do:

self.vWindow = UIApplication.sharedApplication().keyWindow
self.vWindow!.viewWithTag(666).removeFromSuperview()

Upvotes: 1

Related Questions