简体   繁体   English

iOS Swift 3-面板点击后删除叠加式UIView

[英]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. 通过在overlayView中点击按钮来调用函数。 I will include the showOverlay function(working). 我将包括showOverlay函数(工作)。

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. 在for循环之后,但是我担心overlayView.subviews不能正确填充我期望的视图。

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": func showOverlay(_ sender: UIButton) {...}您正在创建一个局部变量“ overlayView”:

let overlayView = UIView(frame: ...)

You then add that as a subview to view . 然后,将其添加为要view的子view All that is fine, except you do not keep a reference to "overlayView" .. the view remains but you have no reference to it. 一切都很好,除非您不保留对“ overlayView” ..视图的引用,但您没有对其的引用。

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: 然后,在func showOverlay(_ sender: UIButton) {...} ,而不是let overlayView =而是将其分配给现有变量:

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: 尝试在类似线程中描述的viewWithTag方法:

Swift addsubview and remove it Swift addsubview并将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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