2017-03-10 8 views
0

UIAlertboxにImageを追加したいので、次のコードを追加します。
Swift 3のUIAlertboxに画像を追加するには?

enter image description here

let alertController = UIAlertController(title: "Gender", message: "" , preferredStyle: .alert) 

    // Create the actions 
     let okAction = UIAlertAction(title: "Female", style: UIAlertActionStyle.default) { 
      UIAlertAction in 
       // exit(0) 
       debugPrint("Press OK") 

     } 
     let cancelAction = UIAlertAction(title: "Male", style: UIAlertActionStyle.cancel) { 
       UIAlertAction in 

     } 


// Add the actions 
    okAction.setValue(#imageLiteral(resourceName: "female_image"), forKey: "image") 
    cancelAction.setValue(#imageLiteral(resourceName: "male_image"), forKey: "image") 
    alertController.addAction(okAction) 
    alertController.addAction(cancelAction) 

私はアプリを実行すると、1つの画像のみが表示されます。これには何が問題なのですか?
誰か助けてください?

答えて

3

はあなただけポッド

をインストールfontAwesomeで試すことができ、画像のサイズを変更するため:-)

swift3

let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert) 

    let image = UIImage(named: "blanckstar") 
    let action = UIAlertAction(title: "Male", style: .default) 
    { 
     UIAlertAction in 
     // exit(0) 
     debugPrint("Press Male") 

    } 
    action.setValue(image, forKey: "image") 

    let image2 = UIImage(named: "blanckstar") 
    let action2 = UIAlertAction(title: "Female", style: .default) 
    { 
     UIAlertAction in 
     // exit(0) 
     debugPrint("Press Female") 

    } 

    action2.setValue(image2, forKey: "image") 

    alertMessage.addAction(action) 
    alertMessage.addAction(action2) 

    self.present(alertMessage, animated: true, completion: nil) 

ハッピーCoadingでその作業このコードを試してみてくださいポッド 'FontAwesome.swift'とインポートFontAwesome_swift

ここでは、コード.....

let alertMessage = UIAlertController(title: "Gender", message: "", preferredStyle: .alert) 

    let image = UIImage.fontAwesomeIcon(name: .male, textColor: UIColor.black, size: CGSize(width: 50, height: 50)) 
    let action = UIAlertAction(title: "Male", style: .default) 
    { 
     UIAlertAction in 
     // exit(0) 
     debugPrint("Press Male") 

    } 
    action.setValue(image, forKey: "image") 

    let image2 = UIImage.fontAwesomeIcon(name: .female, textColor: UIColor.black, size: CGSize(width: 50, height: 50)) 
    let action2 = UIAlertAction(title: "Female", style: .default) 
    { 
     UIAlertAction in 
     // exit(0) 
     debugPrint("Press Female") 

    } 

    action2.setValue(image2, forKey: "image") 

    alertMessage.addAction(action) 
    alertMessage.addAction(action2) 

    self.present(alertMessage, animated: true, completion: nil) 
+0

こんにちは仲間の@seggyで、あなたのコードが動作します!おかげで多くの仲間。しかし、私はイメージのサイズを編集したいと思います。だから、私はこのコードを追加しました。 femaleimgView = UIImageView(フレーム:CGRect(x:10、y:10、width:30、height:30))とfemaleimgView.image = femaleimageにします。 Action1.setValue(femaleimgView、forKey: "image")。このコードを追加すると、エラーが発生します。 : –

+0

私にチェックをさせてください.... – seggy

+0

これはあなたにとって便利ですか? – seggy

関連する問題