簡體   English   中英

在具有透明背景的Alert ViewController的中心添加圖像

[英]Add image on center of Alert ViewController with transparent background

如何在具有警報viewController透明背景的警報viewController中心顯示圖像。 我寫了下面的代碼。

let image = UIImage(named: "ic_no_data")
        let imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)
        let alertMessage = UIAlertController(title: "image", message: "", preferredStyle: .Alert)

        let backView = alertMessage.view.subviews.last?.subviews.last
        backView?.layer.cornerRadius = 10.0
        backView?.backgroundColor = UIColor.lightGrayColor()

        alertMessage.view.addSubview(imageView)

        let action = UIAlertAction(title:"", style: .Cancel, handler: nil)
        action.setValue(image, forKey: "image")
        alertMessage .addAction(action)
        self.presentViewController(alertMessage, animated: true, completion: nil)

看起來像這樣。

在此處輸入圖片說明

請幫助我解決這個問題,並感謝所有人。

您可以將對imageView的引用存儲為alertMessage,並且在完成當前viewController時,您將具有警報視圖的確切大小,因此可以根據當前警報rect調整圖像。

試試這個代碼在swift3中:

 self.present(alertMessage, animated: true, completion: {
            var frame = self.alertMessage.view.frame
            frame.origin = CGPoint(x: 0, y: 0) // add your location
            self.imageView.frame = frame
        })

UIAlertController不支持視圖層次結構中的此類更改,最好使用模仿UIAlertController行為的自定義可擴展組件。 UIAlertController的問題在於您不知道Apple將如何在下一版iOS中更改其視圖層次結構。 而且每次更改時,您都必須為特定版本的iOS添加代碼,從可維護性的角度來看,這確實很糟糕。

例如CustomIOSAlertView: https : //github.com/wimagguc/ios-custom-alertview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM