2016-09-02 61 views
0

我有一个UIActionSheet用于在照相机或照片库之间进行选择,以将图像嵌入到UITextView中,但是无论出于何种原因,它正在加载键盘。我按下了围绕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

我不希望键盘出现,我希望它在出现'UIImagePickerController'时不再出现。 – ggworean

+0

当你完成从图像选择器中选取图像时,出现键盘不存在,你可以删除'self.textView.becomeFirstResponder()'然后'[self.view endEditing:YES];' –

回答

1

找到了解决方案。

我不得不改变

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

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

你可以通过添加一些这方面的变化 -

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()”调用键盘,所以我没有得到你想要的东西你想要的。 –

相关问题