2016-06-16 9 views
1

ActionSheetのUIImageViewで現在の警告をUIAlertControllerにしたいと思います。私はそれが終了のそのアプリケーションを実行する際には....UIIlertControllerでUIImageViewを追加するには?

これは私のコードです:

UIAlertController * alert= [UIAlertController 
            alertControllerWithTitle:@"Title" 
            message:@"Welcome" 
            preferredStyle:UIAlertControllerStyleAlert]; 

    UIAlertAction* okButton = [UIAlertAction 
           actionWithTitle:OK 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 
           }]; 
    [alert addAction:okButton]; 
    UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)]; 
    imgv.image=[UIImage imageNamed:@"kaga.jpg"]; 
    [alert setValue:imgv forKey:@"image"]; 
    [self presentViewController:alert animated:YES completion:nil]; 
+0

それはあなたを助けたら、これを見るhttp://stackoverflow.com/questions/32059305/add-image-to-uialertcontroller問題は、あなたが今直面いただきまし –

答えて

2

AppleのドキュメントによるとUIAlertControllerに画像を追加することはできません。

したい場合は、あなたのような独自のカスタムビューを作成することができます:あなたがイメージがbutton 上に表示撮りたい場合は、次のように試してみてください

https://github.com/wimagguc/ios-custom-alertview

を:

UIAlertController * alert= [UIAlertController 
            alertControllerWithTitle:@"Title" 
            message:@"Welcome" 
            preferredStyle:UIAlertControllerStyleActionSheet]; 

    UIAlertAction* okButton = [UIAlertAction 
           actionWithTitle:OK 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 
//Do some thing here 
         [alert dismissViewControllerAnimated:YES completion:nil]; 

           }]; 

[okButton setValue:[[UIImage imageNamed:@"kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"]; 

[alert addAction:okButton]; 
[self presentViewController:alert animated:YES completion:nil]; 
+0

おかげけど絵が –

+0

が表示されませんか? –

+0

アラートメッセージボックスに画像を表示するには、uialertcontrollerを使用します。しかし、私は、このクラスがキーイメージのキー値コーディングに準拠していないので、[警告setValue:[[UIImage imageNamed:@ "kaga.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@ "image"]; ' –

1

あなたは可能性がありサブタイトルUIAlertControllerをサブクラス化し、タイトル文字列に\nを追加してUIImageViewのスペースを確保して、タイトルラベルの上に画像を追加します。フォントサイズに基づいてレイアウトを計算する必要があります。 のようにKVCを使用してUIAlertActionの画像の場合私はresponds(to:)をチェックする拡張を勧めます。 Here is sample implementation. enter image description here

extension UIAlertAction { 

    /// Image to display left of the action title 
    var actionImage: UIImage? { 
     get { 
      if self.responds(to: Selector(Constants.imageKey)) { 
       return self.value(forKey: Constants.imageKey) as? UIImage 
      } 
      return nil 
     } 
     set { 
      if self.responds(to: Selector(Constants.imageKey)) { 
       self.setValue(newValue, forKey: Constants.imageKey) 
      } 
     } 
    } 

    private struct Constants { 
     static var imageKey = "image" 
    } 
} 
関連する問題