For my app, I want to implement my own AlertController. But I would like to have the same presenting behavior as the UIAlertController does: Make the whole screen a little darker with a translucent background and the alert animates in. I don't want it to appear with a modal presentation.
Ideally, I would like to present my AlertView like the UIAlertView with the present-method, e.g.:
let alert = MyAlertController(type: .example)
present(alert, animated: true, completion: nil)
Edit: Sorry, in my mind it was obviously clear what I wanted but reading the question again I see the issue... So here is what I've tried so far. I created a subclass of UIViewController, below you can see (shortened) code.
Current Behavior: When I present this ViewController, it just presents it in a modal way instead of darkening the screen and popping up my view. I know that I would need to implement the animations, but before I want to get to the
Desired Behavior: I can use my class in the same way as a UIAlertController (see above). I only need to initialize the Controller and then call present. How can I achieve this? It must be somehow possible I think since UIAlertController also uses the mentioned present-method. Thank you!
class FRAlertController: UIViewController {
private var alertView: UIView!
private var alertViewCenterXConstraint: NSLayoutConstraint!
private var alertViewCenterYConstraint: NSLayoutConstraint!
init(type: FRAlertControllerType, buttons: Bool = true) {
super.init(nibName: nil, bundle: nil)
setupView(type: type, buttons: buttons)
}
private func setupView(type: FRAlertControllerType, buttons: Bool) {
setupAlertView()
}
private func setupAlertView() {
alertView = UIView()
alertView.backgroundColor = .white
view.addSubview(alertView)
alertView.translatesAutoresizingMaskIntoConstraints = false
alertViewCenterXConstraint = NSLayoutConstraint(item: alertView!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0)
alertViewCenterYConstraint = NSLayoutConstraint(item: alertView!, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: alertView!, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width
, multiplier: 0.7, constant: 0)
let aspectRatio = NSLayoutConstraint(item: alertView!, attribute: .width, relatedBy: .equal, toItem: alertView, attribute: .height, multiplier: 1.05, constant: 0)
view.addConstraints([
alertViewCenterXConstraint,
alertViewCenterYConstraint,
widthConstraint,
aspectRatio
])
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
}
}
from Own AlertController
No comments:
Post a Comment