2016-09-20 41 views
1

我想將我的xib顯示爲警報視圖。 在xib主視圖中將是半透明的,這將阻止用戶在警報視圖啓動時在背景中的任何其他位置點擊。我沒有在xib中使用視圖控制器。如何在swift中將uiview(xib)作爲警報視圖顯示

+0

這應該是一個簡單的搜索,試試這個http://stackoverflow.com/questions/31997885/how-to-create-custom-uialertcontroller-in-swift-ios –

回答

5

// 1.聲明視圖像這樣的showAlert方法裏面

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

// 2.撰寫方法,比如這樣

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.電話時永遠要展示下。

//showing alert 
ClassName.showAlert() 

//remove alert 
ClassName.removeAlert() 
+0

我沒有使用的UIViewController 。我製作了xib,它是uiview的子類。我已經在xib中創建了我的設計,並且我想將它作爲uialert視圖展示。 –

+0

我編輯了我的答案。請檢查。 – Vishnuvardhan

+0

非常感謝你Vishnuvardhan.Its nice –

相關問題