Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created February 15, 2018 19:51
Show Gist options
  • Select an option

  • Save DonMag/e29fcfcac6a9acef102f1914b2adb640 to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/e29fcfcac6a9acef102f1914b2adb640 to your computer and use it in GitHub Desktop.
class C2ViewController: UIViewController {
var myCustomAlertView = CustomAlertView()
override func viewDidLoad() {
super.viewDidLoad()
if var topController = UIApplication.shared.delegate?.window??.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
myCustomAlertView = CustomAlertView("Here is a message - Hello!")
myCustomAlertView.translatesAutoresizingMaskIntoConstraints = false
topController.view.addSubview(myCustomAlertView)
myCustomAlertView.topAnchor.constraint(equalTo: topController.view.topAnchor, constant: 0.0).isActive = true
myCustomAlertView.bottomAnchor.constraint(equalTo: topController.view.bottomAnchor, constant: 0.0).isActive = true
myCustomAlertView.leadingAnchor.constraint(equalTo: topController.view.leadingAnchor, constant: 0.0).isActive = true
myCustomAlertView.trailingAnchor.constraint(equalTo: topController.view.trailingAnchor, constant: 0.0).isActive = true
}
}
}
class CustomAlertView: UIView {
lazy var theLabel: UILabel = {
let v = UILabel()
v.backgroundColor = .yellow
v.textAlignment = .center
v.numberOfLines = 0
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
convenience init(_ withString: String) {
self.init(frame: CGRect.zero)
theLabel.text = "The passed string is:\n\n\(withString)"
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .black
alpha = 0.60
addSubview(theLabel)
theLabel.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0.0).isActive = true
theLabel.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0.0).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment