0

I am very new to swift and I am stuck with the task described in the title.

My Problem:

I am building a product page programmatically, consisting of a few simple details and an offer button. Tapping offer button brings up an overlay on the view with some other details. You click "Ok" and the overlay disappears.

All good except the overlay does not disappear!

What I have tried:

func hideOverlay(_ sender: UIButton) {
    containerView.backgroundColor = UIColor.white
    buttonView.backgroundColor = UIColor.white
    for subview in overlayView.subviews {
        subview.removeFromSuperview()
    }
}

Function is called on tapping the button within the overlayView. I will include the showOverlay function(working).

func showOverlay(_ sender: UIButton) {
    //Load overlay view
    let overlayHeight : CGFloat = 500
    let overlayWidth : CGFloat = 290
    let overlayView = UIView(frame: CGRect(x: centreView(masterView: view.frame.width, subView: overlayWidth), y: 64 + centreView(masterView: (view.frame.height - 64), subView: overlayHeight), width: overlayWidth, height: overlayHeight))
    overlayView.backgroundColor = UIColor.white

    let overlayTitle = UILabel(frame: CGRect(x: 0, y: 0, width: overlayWidth, height: overlayHeight*1/5))
    overlayTitle.text = "Offer Taken"
    overlayTitle.font = UIFont.boldSystemFont(ofSize: 35)
    overlayTitle.textAlignment = .center
    overlayView.addSubview(overlayTitle)

    let overlayButtonView = UIView(frame: CGRect(x: 0, y: 0 + (overlayHeight * 4/5), width: overlayWidth, height: overlayHeight * 1/5))
    overlayButtonView.backgroundColor = UIColor.red

    let buttonWidth : CGFloat = 100
    let buttonHeight : CGFloat = 35
    let overlayButton = UIButton(type: UIButtonType.system)
    overlayButton.frame = CGRect(x: centreView(masterView: overlayWidth, subView: buttonWidth), y: overlayButtonView.frame.origin.y + centreView(masterView: overlayButtonView.frame.height, subView: buttonHeight), width: buttonWidth, height: buttonHeight)
    overlayButton.backgroundColor = UIColor.blue
    overlayButton.setTitle("OK",for: .normal)
    overlayButton.setTitleColor(UIColor.white, for: .normal)
    overlayButton.setTitle("Offer Taken", for: .highlighted)
    overlayButton.setTitleColor(UIColor.white, for: .highlighted)
    overlayButton.addTarget(self, action: #selector(self.hideOverlay(_:)), for: .touchUpInside)
    overlayView.addSubview(overlayButtonView)
    overlayView.addSubview(overlayButton)

    containerView.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.5)
    buttonView.backgroundColor = UIColor(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.5)
    view.addSubview(overlayView)        
}

I have tried

overlayView.removeFromSuperview()

after the for loop, but I fear that overlayView.subviews is not correctly filled with the views I expect.

I appreciate anyone taking the time to help me, even if a little closer to a solution.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Have you checked to see if your remove code is actually getting run? – adamfowlerphoto May 24 '17 at 14:56
  • There was no subviews of the overlayView. I thought that overlayView.addSubview(blah) would generate some subviews but it didn't seem to. The answer below with tags did the trick though, ty for your time – ADoorMarkedPirate May 24 '17 at 15:05

2 Answers2

2

In func showOverlay(_ sender: UIButton) {...} you are creating a local variable "overlayView":

let overlayView = UIView(frame: ...)

You then add that as a subview to view. All that is fine, except you do not keep a reference to "overlayView" .. the view remains but you have no reference to it.

Add a class-level variable, outside of any function blocks:

var overlayView: UIView!

Then, inside func showOverlay(_ sender: UIButton) {...}, instead of let overlayView = just assign it to the existing variable:

overlayView = UIView(frame: ...)

When you're ready to remove it:

func hideOverlay(_ sender: UIButton) {
    overlayView.removeFromSuperview()
}

and you're done :)

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • This is what I was looking for, thank you. I had assigned a class variable (I think atleast) doing var overlayView = UIView(). But then I guess I had overwritten it by doing let overlayView = UIView(frame: ...)? I made the adjustments here and it worked a treat – ADoorMarkedPirate May 24 '17 at 15:13
  • Exactly... anytime you use `let` or `var` you are creating a *new* variable. If you do so in a "lower scope" - such as in a function - that *new* variable will be used instead of the higher-scope variable. Glad you got it working :) – DonMag May 24 '17 at 15:19
0

Try the viewWithTag method described in a similar thread here:

Swift addsubview and remove it

fgroeger
  • 422
  • 5
  • 18