Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自定义UIView赢得';不显示_Ios_Swift - Fatal编程技术网

Ios 自定义UIView赢得';不显示

Ios 自定义UIView赢得';不显示,ios,swift,Ios,Swift,我试图创建一个自定义视图,在调用函数时应该可见 我有一个文件名为:customPopUp.swift 该文件中包含以下代码: import UIKit class customPopUp: UIView { let customPopUp = UIView(frame: CGRect.init(x: 25, y: ((UIScreen.main.bounds.height / 2) - 300), width: (UIScreen.main.bounds.width - 50), h

我试图创建一个自定义视图,在调用函数时应该可见

我有一个文件名为:customPopUp.swift

该文件中包含以下代码:

import UIKit

class customPopUp: UIView {

    let customPopUp = UIView(frame: CGRect.init(x: 25, y: ((UIScreen.main.bounds.height / 2) - 300), width: (UIScreen.main.bounds.width - 50), height: 400))
    var popUpImageView: UIImageView!
    var headlinePopUp: UILabel!
    var textPopUp: UILabel!
    var okayButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func customPop(image: String, headline: String, text: String, buttonText: String) {
        self.customPopUp.backgroundColor = .white
        self.customPopUp.layer.cornerRadius = 15
        self.customPopUp.layer.shadowColor = UIColor.black.cgColor
        self.customPopUp.layer.shadowOpacity = 1
        self.customPopUp.layer.shadowOffset = CGSize.zero
        self.customPopUp.layer.shadowOpacity = 0.5
        self.customPopUp.layer.shadowRadius = 5
        self.addSubview(customPopUp)

        customPopUp.isHidden = false

        popUpImageView = UIImageView(frame: CGRect.init(x: ((customPopUp.frame.size.width / 2) - 80), y: 25, width: 160, height: 160))
        popUpImageView.image = UIImage(named: image)
        self.customPopUp.addSubview(popUpImageView)

        headlinePopUp = UILabel(frame: CGRect.init(x: 15, y: 230, width: (customPopUp.frame.size.width - 30), height: 25))
        headlinePopUp.font = UIFont.init(name: "FiraSans-Bold", size: 20)
        headlinePopUp.textColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1)
        headlinePopUp.textAlignment = .center
        headlinePopUp.text = headline
        self.customPopUp.addSubview(headlinePopUp)

        textPopUp = UILabel(frame: CGRect.init(x: 15, y: 260, width: (customPopUp.frame.size.width - 30), height: 40))
        textPopUp.font = UIFont.init(name: "DroidSans", size: 14)
        textPopUp.textColor = UIColor(red: 150/255, green: 150/255, blue: 150/255, alpha: 1)
        textPopUp.textAlignment = .center
        textPopUp.text = text
        textPopUp.numberOfLines = 2
        self.customPopUp.addSubview(textPopUp)

        okayButton = UIButton(frame: CGRect.init(x: ((customPopUp.frame.size.width / 2) - 100), y: 320, width: 200, height: 45))
        okayButton.titleLabel?.font = UIFont.init(name: "FiraSans-Bold", size: 16)
        okayButton.setTitle(buttonText, for: .normal)
        okayButton.backgroundColor = UIColor(red: 80/255, green: 8/255, blue: 8/255, alpha: 1)
        okayButton.titleLabel?.textColor = .white
        okayButton.layer.cornerRadius = 5
        okayButton.clipsToBounds = true
        okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView(sender:)), for: .touchUpInside)
        self.customPopUp.addSubview(okayButton)

        self.customPopUp.alpha = 0.0
        popUpImageView.alpha = 0.0
        headlinePopUp.alpha = 0.0
        textPopUp.alpha = 0.0
        okayButton.alpha = 0.0

        UIView.animate(withDuration: 0.2, animations: {
            self.customPopUp.alpha = 1.0
            self.popUpImageView.alpha = 1.0
            self.headlinePopUp.alpha = 1.0
            self.textPopUp.alpha = 1.0
            self.okayButton.alpha = 1.0
        })
    }

    @objc func didPressButtonFromCustomView(sender:UIButton) {
        // do whatever you want
        // make view disappears again, or remove from its superview
        UIView.animate(withDuration: 0.2, animations: {
            self.customPopUp.alpha = 0.0
            self.popUpImageView.alpha = 0.0
            self.headlinePopUp.alpha = 0.0
            self.textPopUp.alpha = 0.0
            self.okayButton.alpha = 0.0
        })
        customPopUp.isHidden = true
    }

}
在我的视图控制器中,我调用文件中的函数,如下所示:

let thePopUp = customPopUp()
            thePopUp.customPop(image: "wrong", headline: "No entrance", text: "You don't belong here", buttonText: "Okay")

但是,当像上面那样调用函数时,什么也不显示。我没有得到错误,我没有得到视图或任何东西。我做错了什么?

您必须在任何位置为弹出窗口提供帧

    let rect = CGRect(x: 10, y: 70, width: 250, height: 400)
    let thePopUp = customPopUp(frame: rect)
    self.view.addSubview(thePopUp )

我在前几行就这么做了
let customPopUp=UIView(frame:CGRect.init(x:25,y:((UIScreen.main.bounds.height/2)-300),width:(UIScreen.main.bounds.width-50),height:400))
I添加了
self.view.addSubview(thePopUp)
,现在它显示了我想要的元素。但当我点击按钮时,什么都没发生?当我按下按钮时,问题代码底部的动作函数甚至没有被调用。我不知道为什么,因为我在代码中添加了目标?你是说OK按钮??在那里放一条打印信息