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

廉价感情. 提交于 2021-02-07 14:31:30

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/39586656/how-can-i-present-uiview-xib-as-alert-view-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!