Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 807
Pedro Manfredi Avatar asked Aug 19 '14 09:08

Pedro Manfredi


Video Answer


2 Answers

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:

  • How would I create a UIAlertView in Swift?

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.

like image 62
Suragch Avatar answered Oct 19 '22 23:10

Suragch


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:

enter image description here

Effect using a UIView with 70 Alpha:

enter image description here

like image 33
fs_tigre Avatar answered Oct 20 '22 00:10

fs_tigre