简体   繁体   English

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

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

How to display image in center of alert viewController with transparent background of alert viewController. 如何在具有警报viewController透明背景的警报viewController中心显示图像。 I wrote following code. 我写了下面的代码。

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)

It looks like this. 看起来像这样。

在此处输入图片说明

Please help me to solve this problem and thanks to all. 请帮助我解决这个问题,并感谢所有人。

you can store you reference of imageView an alertMessage, and in completion of the present viewController you have the exact size of the alert view, so you can adjust the image according to the current alert rect. 您可以将对imageView的引用存储为alertMessage,并且在完成当前viewController时,您将具有警报视图的确切大小,因此可以根据当前警报rect调整图像。

try this code is in swift3 : 试试这个代码在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 doesn't support such changes in view hierarchy you'd be better using a custom extensible component that mimics the behavior of UIAlertController. UIAlertController不支持视图层次结构中的此类更改,最好使用模仿UIAlertController行为的自定义可扩展组件。 The problem with UIAlertController is that you don't know how Apple will change its view hierarchy in the next version of iOS. UIAlertController的问题在于您不知道Apple将如何在下一版iOS中更改其视图层次结构。 And each time they change something you'll have to add code for a specific version of iOS which is really bad from the maintainability point of view. 而且每次更改时,您都必须为特定版本的iOS添加代码,从可维护性的角度来看,这确实很糟糕。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 推ViewController与透明背景 - Push ViewController with transparent background 情节提要ViewController中的透明背景 - transparent background in storyboard viewcontroller iOS:具有透明背景的Modal ViewController - iOS: Modal ViewController with transparent background 如何使Popup ViewController的背景透明? - How to make a transparent background of Popup ViewController? ViewController 显示为透明并在后台显示另一个屏幕 - ViewController is appearing transparent and showing another screen in background 如何将 ViewController 呈现为具有透明背景的弹出式 VC? - How to present ViewController as popup VC with transparent background? 如何推送具有透明背景的新viewController? - How to push a new viewController with Transparent Background? iOS 7:透明的Viewcontroller背景在另一个具有不同方向行为的Viewcontroller上 - IOS 7: Transparent Viewcontroller Background over another Viewcontroller with different Orientation behaviour 如何将一个ViewController或View加载到具有透明背景的另一个ViewController中? - How can I load a ViewController or View into another ViewController with transparent background? 在ViewController中设置UINavigationBar背景图像 - Set UINavigationBar background image in ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM