1

i have been searching for a long time to make custom Alert Views myself. But there was no success. What i do is i design a view in the xib file in the story board and load it in the application but it just shows up like regular views. What i want to do is present this view as AlertController in iOS >= 8 using Swift 3. What i did is

let alert = Bundle.main.loadNibNamed("xib file to load", owner: self, options: nil)?.last as! UIView

// showAlert(alert: alert)
func showAlert(alert: UIView){
    let windows = UIApplication.shared.windows
    let lastWindow = windows.last
    print(lastWindow)
    alert.frame = CGRect(x: 0, y: 0, width: 150, height: 300)
    alert.center = self.view.center
    lastWindow?.addSubview(alert)
}

func removeAlert(alert: UIView){
    alert.removeFromSuperview()
}

This just shows and hides the view. But i want it to present like UIAlertController. I want everything in the background dim a and only focus on this view. How can i achieve that. Please help me through this.

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
Salman Ali
  • 255
  • 8
  • 25

3 Answers3

3

First of all you should never touch window on iOS. Except for extremely rare special cases.

You can create in your .xib a view with transparent or semi-transparent background. Then you can define a protocol to present this view:

protocol AlertPresenting {}

extension AlertPresenting {
    func showAlert(_ onView: UIView) {
        if let alert = Bundle.main.loadNibNamed("AlertView", owner: nil, options: nil)![0] as? AlertView {
            alert.translatesAutoresizingMaskIntoConstraints = false

            onView.addSubview(alert)
            //here define constraints
            onView.bringSubviewToFront(alert)
        }
    }
}

Then you can use it on any viewController:

class MyViewController: UIViewController, AlertPresenting {
    func someFunction() {
        showAlert(onView: self.view)
    }
}

The alert will be above all the views.

Max Pevsner
  • 4,098
  • 2
  • 18
  • 32
  • will this alert be like alertController? Means everything in the background will be dimmed and the alert will look like its raised a little? – Salman Ali Jul 11 '17 at 08:26
  • @SalmanAli you should create the semi-transparent background yourself, as I wrote in the answer. You custom `alertView` should cover the entire screen, but non-transparent view (the alert you want to show) should be smaller. – Max Pevsner Jul 11 '17 at 08:51
0
let alert = UIAlertController(title: "Record Inserted", message: 
"Record Inserted", preferredStyle: UIAlertController.Style.alert)

alert.addAction(UIAlertAction(title: "Ok", 
style:UIAlertAction.Style.default, handler: nil))

self.present(alert, animated: true, completion: nil)
Miral Kamani
  • 11
  • 1
  • 3
0

Create a view controller in storyboard with transparent background view and then present it

let storyBoard = UIStoryboard(name: "Alert", bundle: .main)
let viewController : UIViewController = storyBoard.instantiateViewController(withIdentifier: "AlertController")

customAlertController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
customAlertController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve

self.present(customAlertController, animated: true, completion: nil)
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57