【问题标题】:Swift , hide/remove a window (View?)Swift,隐藏/删除窗口(查看?)
【发布时间】:2018-12-22 19:16:18
【问题描述】:

我想删除我在以下代码中创建的视图(窗口 keyWindow 或 v UIView)。

我尝试使用适用于视图的 .tag,但它不起作用。

为了解释代码的用途,它使用 Firebase 测试是否有互联网连接;如果没有,屏幕中间会出现一个 no wifi gif;如果是,则 no wifi gif 应该消失(但在我的情况下不是)。

在这里,它正确显示带有窗口 (v) 的 gif,当它获得连接时,它会尝试测试以删除视图,但它什么也不做

这段代码应该适用于我的应用程序的每个 uiviewcontroller(这是我的目标),但现在我正在使用 viewcontroller 上的本地函数对其进行测试

编辑:这里是感兴趣的人的固定代码:)

 func testconnection() {



        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
            let connectedRef = Database.database().reference(withPath: ".info/connected")
            connectedRef.observe(.value, with: { snapshot in
                if let connected = snapshot.value as? Bool, connected {
                    print("Connected")
                    let window = UIApplication.shared.keyWindow!
                    let v = UIView(frame: CGRect(x: self.view.center.x-50, y: self.view.center.y-10, width: 100, height: 100))
                    v.backgroundColor = UIColor.black
                    v.tag = 100
                    print("Start testing to remove the view with correct tag")
                    if let viewWithTag = window.viewWithTag(100) {
                        print("yes, remove view")
                        viewWithTag.removeFromSuperview()
                    }else{
                        print("Not the correct tag / view not created")
                    }



                }
                else {

                    print ("Not connected")


                    SDWebImageCodersManager.sharedInstance().addCoder(SDWebImageGIFCoder.shared())
                    let window = UIApplication.shared.keyWindow!
                    let v = UIView(frame: CGRect(x: self.view.center.x-50, y: self.view.center.y-10, width: 100, height: 100))
                    window.addSubview(v);
                    let imageData = try? Data(contentsOf: Bundle.main.url(forResource: "noWifi1", withExtension: "gif")!)
                    let advTimeGif = UIImage.gifImageWithData(imageData!)
                    let imageView2 = UIImageView(image: advTimeGif)
                    imageView2.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
                    imageView2.tag = 100
                    v.addSubview(imageView2)




                }
            })
        })
    }

【问题讨论】:

    标签: swift firebase uiview connection window


    【解决方案1】:

    它不起作用,因为您将v 添加到window,而不是self.view;因此,viewWithTagnil。要解决此问题,请将 self.view.viewWithTag(100) 更改为 window.viewWithTag(100)

    viewWithTag(_:) 将查看接收者的视图层次结构以查找具有匹配标签的视图。在您的情况下,v 不在self.view 的层次结构中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      相关资源
      最近更新 更多