iosswiftuialertview

Custom Alert (UIAlertView) with swift


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?


Solution

  • Code tested in Swift 5 and Xcode 10

    How to make your own custom Alert

    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.

    enter image description here

    Storyboard

    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.)

    enter image description here

    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.

    enter image description here

    Code

    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.

    enter image description here

    Other options

    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.