2016-09-02 5 views
0

UITextViewに画像を埋め込むためにカメラまたは写真ライブラリを選択するには、UIActionSheetがありますが、何らかの理由でキーボードがロードされています。 UITextViewを囲むバーの左ボタンを押すとキーボードが強制的に閉じられますが、写真ライブラリを押すと、画像ピッカーVCにプッシュする前にキーボードが開閉します。UIImagePickerControllerビューを読み込むたびにキーボードが読み込まれるのはなぜですか?

override func didPressLeftButton(sender: AnyObject?) { 
    let cameraMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 

    let photoLibrary = UIAlertAction(title: "Photo Library", style: .Default, handler: { (UIAlertAction) in 
     self.openPhotoLibrary() 
    }) 

    let takePhoto = UIAlertAction(title: "Open Camera", style: .Default, handler: { (UIAlertAction) in 
     self.textView.endEditing(true) 
     self.openCamera() 
    }) 

    let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 

    cameraMenu.addAction(photoLibrary) 
    cameraMenu.addAction(takePhoto) 
    cameraMenu.addAction(cancel) 

    self.presentViewController(cameraMenu, animated: true, completion: nil) 
} 

func openPhotoLibrary() { 
    imagePicker.sourceType = .PhotoLibrary 
    imagePicker.allowsEditing = false 
    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func openCamera(){ 
    imagePicker.sourceType = .Camera 
    imagePicker.showsCameraControls = true 
    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     // Image resizing 
     let textViewWidth: CGFloat = self.textView.frame.size.width - 20 
     let percentResize = textViewWidth/pickedImage.size.width 
     let toBeExportedHeight = pickedImage.size.height * percentResize 
     let resizedImage = ImageManipulationManager.sharedInstance.resizeImage(exportedWidth: Int(textViewWidth),exportedHeight: Int(toBeExportedHeight), originalImage: pickedImage) 

     // Storage into TextView 
     let attachment = NSTextAttachment() 
     attachment.image = resizedImage 
     let attString = NSAttributedString(attachment: attachment) 
     textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location) 
     pastedImageLocations.append(textView.selectedRange.location) 
     textView.selectedRange.location = textView.selectedRange.location + 1 
     textView.textStorage.insertAttributedString(NSAttributedString(string: "\n"), atIndex: textView.selectedRange.location) 
     textView.selectedRange.location = textView.selectedRange.location + 1 
     textView.font = UIFont.systemFontOfSize(16.0) 

     // Image Caching 
     if let data = UIImageJPEGRepresentation(pickedImage, 0.50) { 
      socketMessages.append(["data": data]) 
      haneke.set(value: data, key: String(unsafeAddressOf(attachment.image!))) 
      print("Image cached as \"\(String(unsafeAddressOf(attachment.image!)))\"") 
     } 
    } 
    dismissViewControllerAnimated(true, completion: nil) 
    self.textView.becomeFirstResponder() 
} 
+0

self.textView.becomeFirstResponderに()キーボード –

+0

を開きますIキーボードが表示されないようにするには、 'UIImagePickerController'を表示すると表示されなくなるようにします。 – ggworean

+0

イメージピッカーからイメージをピックし終わったらキーボードが現れ、 'self.textView.becomeFirstResponder()'と '[self.view endEditing:YES];を削除することができます; –

答えて

1

解決策が見つかりました。

私はあなたがこれを追加することによって、いくつかの変更を行うことができます

dismissViewControllerAnimated(true) { 
     self.textView.becomeFirstResponder() 
    } 
0

dismissViewControllerAnimated(true, completion: nil) 
self.textView.becomeFirstResponder() 

を変更しなければならなかった -

override func didPressLeftButton(sender: AnyObject?) { 
    let cameraMenu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet) 

    let photoLibrary = UIAlertAction(title: "Photo Library", style: .Default, handler: { (UIAlertAction) in 
     self.view.endEditing(true) //**------ Add this 
     self.openPhotoLibrary() 
    }) 

    let takePhoto = UIAlertAction(title: "Open Camera", style: .Default, handler: { (UIAlertAction) in 
     self.view.endEditing(true) //**------ Add this 
     self.openCamera() 
    }) 

    let cancel = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 

    cameraMenu.addAction(photoLibrary) 
    cameraMenu.addAction(takePhoto) 
    cameraMenu.addAction(cancel) 

    self.presentViewController(cameraMenu, animated: true, completion: nil) 
} 
+0

openCamera()で 'endEditing()'を呼び出すと、問題はありませんでした。問題は次のように表示されます – ggworean

+0

あなたが問題に直面している私に教えてください画像の終わりにもう一度 "self.textView.becomeFirstResponder()"キーボードを呼び出すので、私はあなたが何をしたいあなたのポイントを取得していません。 –

関連する問題