我试图创建一个在调用函数时应该可见的自定义视图。
我有一个名为: 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")
但是当像上面那样调用函数时,没有任何显示。我没有得到错误,我没有得到视图或任何东西。我做错了什么?
答案 0 :(得分:0)
你必须在任何地方为弹出框提供框架
let rect = CGRect(x: 10, y: 70, width: 250, height: 400)
let thePopUp = customPopUp(frame: rect)
self.view.addSubview(thePopUp )