Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i present uiview (xib) as alert view in swift

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.

like image 584
Ankit Kr.Vishwakarma Avatar asked Sep 20 '16 05:09

Ankit Kr.Vishwakarma


People also ask

How do I show Xib as popup in Swift?

(Command + Shift + L) UITableView and drag and drop to the xib window & design cell according your project requirement.

How do I create a custom alert view in Swift?

Step3: In Main. Storyboard, drag a new UIViewController, and assign the class AlertViewController to it. Step4: Now add the custom alert design of your choice, check below my project's design. Step5: Make the outlet of views in AlertViewController, here's below how your AlertViewController will look like.


1 Answers

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()
like image 114
Vishnuvardhan Avatar answered Oct 03 '22 00:10

Vishnuvardhan