在主故事板上有一个带有按钮的视图控制器。还有一个Swift文件(Helper.swift)。
ViewController.swift文件:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openView(_ sender: UIButton) {
pop_Up_A_View()
}
}
Helper.swift文件:
import Foundation
import UIKit
func pop_Up_A_View()
{
let window = UIApplication.shared.keyWindow!
let v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
window.addSubview(v);
v.backgroundColor = UIColor.black.withAlphaComponent(0.7)
let indicatorTemp = UIActivityIndicatorView (frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
indicatorTemp.startAnimating()
indicatorTemp.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
window.addSubview(indicatorTemp)
}
如您所见,我通过调用pop_Up_A_View
函数看到带有活动指示器的视图。如何使用活动指示器消除此视图?我试图将这段代码编写为close_Sub_View
函数,但我不知道。
实际上,我应该再执行一个案例。如果已经有一个打开的子视图,则pop_Up_A_View
函数不应再打开一个子视图。
答案 0 :(得分:0)
您可以将您的视图标签识别出来并删除。您还必须将指标视图添加到视图v内,而不是直接添加窗口
使用以下内容
func pop_Up_A_View(){
let window = UIApplication.shared.keyWindow!
if let view = window.viewWithTag(1000) as? UIView {
return
}
let v = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
v.tag = 1000
v.backgroundColor = UIColor.black.withAlphaComponent(0.7)
let indicatorTemp = UIActivityIndicatorView (frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
indicatorTemp.startAnimating()
indicatorTemp.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
v.addSubview(indicatorTemp)
window.addSubview(v);
}
func close()
{
let window = UIApplication.shared.keyWindow!
if let view = window.viewWithTag(1000) {
(view as! UIView).removeFromSuperview()
}
}