简体   繁体   中英

iOS Swift 3 - Remove Overlay UIView after Panel Tap

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.

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 :)

Try the viewWithTag method described in a similar thread here:

Swift addsubview and remove it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM