6

I want to present the my xib as the alert view. In the xib main view is going to be semi-transparent, which will prevent users from tapping on anything else in the background while the alert view is up. I am not using the view controller in xib.

  • this should have been an easy search, try this http://stackoverflow.com/questions/31997885/how-to-create-custom-uialertcontroller-in-swift-ios – Umair Afzal Sep 20 '16 at 06:12

1 Answers1

8

1.Fetch the XIB file object.

let alert = NSBundle.mainBundle().loadNibNamed("Alert", owner: self, options: nil).last as! UIView     

2.Compose the convenience methods.

static func showAlert() {
    let windows = UIApplication.sharedApplication().windows
    let lastWindow = windows.last
    alert.frame = UIScreen.mainScreen().bounds
    lastWindow?.addSubview(alert)
}

static func removeAlert() {
    alert.removeFromSuperview()
}

3.Call the methods.

//showing alert
ClassName.showAlert()

//remove alert
ClassName.removeAlert()
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • i am not using the uiviewcontroller. i have make the xib and it is subclass of uiview. i have created my design in the xib and i want to present it as the uialert view.can i??? – Ankit Kr.Vishwakarma Sep 20 '16 at 06:20