[swift] UIAlertControllerに画像を追加する

UIAlertControllerに画像を追加する

Alertを画面に表示させるUIAlertControllerは基本的に文字のみだが、画像をUIAlertController上に描画することができる

サンプルコード

        let title = "GPS Course Creation"
        let message = "Can create your course by GPS"
        
        let alert = UIAlertController(title:title,
                                      message: message,
                                      preferredStyle: UIAlertControllerStyle.Alert)
        
        
        let cancelAction:UIAlertAction = UIAlertAction(title: "OK",
                                                       style: UIAlertActionStyle.Cancel,
                                                       handler:{
                                                        action in
        })
        alert.addAction(cancelAction)
        let imageView = UIImageView(frame: CGRectMake(5, -40, 60, 60))
        imageView.image = UIImage(named: "alert_gps.png")
        alert.view.addSubview(imageView)
        self.presentViewController(alert, animated: true, completion: nil)

結果:
swift-uialertcontroller%e3%81%ab%e7%94%bb%e5%83%8f%e3%82%92%e8%bf%bd%e5%8a%a0%e3%81%99%e3%82%8b

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です