How can i create a custom alert with Swift? I try translating a guide from Objective c but loads a full screen layout
for do it easy i can load a new layout with the transparent background i try this:
listaalertviewcontroller.view.backgroundColor = UIColor.clearColor() let purple = UIColor.purpleColor() // 1.0 alpha let semi = purple.colorWithAlphaComponent(0.5) listaalertviewcontroller.view.backgroundColor = semi presentingViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext self.presentViewController(listaalertviewcontroller, animated: true, completion: nil)
in the animation it's transparent but when the animation ends it's opaque... and i turn off opaque option in the view... what i'm doing wrong?
Code tested in Swift 5 and Xcode 10
I was wanting to do something similar. First of all, UIAlertView
is deprecated in favor of UIAlertController
. See this answer for the standard way to display an alert:
And both UIAlertView
and UIAlertController
do not really allow much customization. One option is to use some third party code. However, I discovered that it isn't that difficult to create your own Alert by displaying another view controller modaly.
The example here is just a proof-of-concept. You can design your alert any way you want.
You should have two View Controllers. Your second view controller will be your alert. Set the class name to AlertViewContoller
and the Storyboard ID to alert
. (Both of these are names that we defined ourselves in the code below, nothing special about them. You can add the code first if you want. It might actually be easier if you add the code first.)
Set the background color for the root view (in your Alert View Controller) to clear (or translucent black is nice for an alert). Add another UIView
and center it with constraints. Use that as your alert background and put whatever you want inside. For my example, I added a UIButton
.
ViewController.swift
import UIKit class ViewController: UIViewController { @IBAction func showAlertButtonTapped(_ sender: UIButton) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let myAlert = storyboard.instantiateViewController(withIdentifier: "alert") myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve self.present(myAlert, animated: true, completion: nil) } }
AlertViewController.swift
import UIKit class AlertViewController: UIViewController { @IBAction func dismissButtonTapped(_ sender: UIButton) { self.dismiss(animated: true, completion: nil) } }
Don't forget to hook up the outlets.
You can add an onTouchUp
event listener to the background view to dismiss the popup when the user clicks outside of it.
That's it. You should be able to make any sort of alert that you can imagine now. No need for third party code.
Here is another custom alert I made. Still ugly, but it shows more things you can do.
Sometimes there is no need to reinvent the wheel, though. I'm impressed with the third party project SDCAlertView (MIT license). It is written in Swift but you can use it with Objective-C projects as well. It offers a wide range of customability.
Here is the Swift 3 code. Thanks a lot @Suragch for the awesome approach to create a custom AlertView.
ViewController.swift
import UIKit class ViewController: UIViewController { @IBAction func showAlertButtonTapped(sender: UIButton) { let storyboard = UIStoryboard(name: "Main", bundle: nil) let myAlert = storyboard.instantiateViewController(withIdentifier: "storyboardID") myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve self.present(myAlert, animated: true, completion: nil) }
AlertViewController.swift
import UIKit class AlertViewController: UIViewController { @IBAction func dismissButtonTapped(sender: UIButton) { self.dismiss(animated: true, completion: nil) } }
To make it a little more interesting or to make the default effect in iOS, you could add either a VisualEffectView or change the color of the main UIView to a dark color and set its alpha to 70%. I prefer the second approach since the blur effect is not as smooth as the one with the view with 70 alpha.
Effect with VisualEffectView:
Effect using a UIView with 70 Alpha:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With