iOS Swift 3 - 面板点击后删除覆盖 UIView

标签 ios swift uiview

我是 swift 的新手,我被标题中描述的任务困住了。

我的问题:

我正在以编程方式构建一个产品页面,其中包含一些简单的详细信息和一个报价按钮。点击报价按钮会在 View 上显示一些其他详细信息的叠加层。您单击“确定”,叠加层消失。

一切都很好,除了覆盖层不会消失!

我尝试过的:

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

在 overlayView 中点击按钮时调用函数。我将包含 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)        
}

我试过了

overlayView.removeFromSuperview()

在 for 循环之后,但我担心 overlayView.subviews 没有正确填充我期望的 View 。

我感谢任何花时间帮助我的人,即使离解决方案更近了一点。

最佳答案

func showOverlay(_ sender: UIButton) {...} 中,您正在创建一个本地变量“overlayView”:

let overlayView = UIView(frame: ...)

然后将其作为 subview 添加到 view。一切都很好,除了您不保留对“overlayView”的引用.. View 仍然存在,但您没有引用它。

在任何功能 block 之外添加一个类级变量:

var overlayView: UIView!

然后,在 func showOverlay(_ sender: UIButton) {...} 中,而不是 let overlayView = 只是将其分配给现有变量:

overlayView = UIView(frame: ...)

当您准备好删除它时:

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

大功告成:)

关于iOS Swift 3 - 面板点击后删除覆盖 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44161845/

相关文章:

UITableView 位于顶部

iphone - UIView 子类中的事件处理

ios - 尝试使用自定义类将值传递给 UIView 时,UISlider 值为零

iphone - 如何在 iOS 中为应用程序图标制作动画?

ios - Xamarin IOS识别设备

php - 如何将 "chat message"从 iOS TextField 发送到远程数据库

ios - Swift:ViewController 中的 TableView

swift -\u{...} 转义序列中的预期 '}'

ios - 尝试复制 UIView 的框架以负偏移结束

ios - xCode 中的 Firebase.configure() 导致线程 1 : signal SIGABRT : terminating with uncaught exception of type NSException on App Delegate