简体   繁体   English

如何在swift中添加一个按钮来访问照片库到键盘?

[英]How to add a button to access photo gallery to keyboard in swift?

I'm building a chat app and I'd like to add the opportunity to send image. 我正在构建一个聊天应用程序,我想添加发送图像的机会。 That's why I'd like to add a shortcut on the keyboard to access gallery or phone's camera. 这就是为什么我想在键盘上添加一个快捷方式来访问图库或手机的相机。

I don't see it in the native keyboard type ( https://developer.apple.com/documentation/uikit/uikeyboardtype ) so I guess I've to do it programatically. 我没有在原生键盘类型( https://developer.apple.com/documentation/uikit/uikeyboardtype )中看到它,所以我想我要以编程方式进行。

I've found nothing about it on the stackOverflow nor on the internet. 我在stackOverflow上和互联网上都没有发现它。

I'd like to have access to a keyboard like the one you can have in iMessage for example. 我想要访问类似于iMessage中可用的键盘。

You can create your own view above the keyboard by editing NSLayoutConstraint constants. 您可以通过编辑NSLayoutConstraint常量在键盘上方创建自己的视图。 Here is an example storyboard. 这是一个示例故事板。

storyboard and constraints 故事板和约束

Then: 然后:

  • Connect your NSLayoutConstraint to the View Controller 将NSLayoutConstraint连接到View Controller

@IBOutlet weak var overKeyboardViewBottomConstraint: NSLayoutConstraint!

  • Add keyboard listeners in your viewDidLoad method: 在viewDidLoad方法中添加键盘侦听器:

      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil) 
  • Create selectors for keyboard 为键盘创建选择器

    @objc func keyboardWillShow(notification: Notification) { if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { UIView.animate(withDuration: 0.5) { [unowned self, keyboardSize] in self.overKeyboardViewBottomConstraint.constant = keyboardSize.height self.view.layoutIfNeeded() } } } @objc func keyboardWillHide(notification: Notification) { UIView.animate(withDuration: 0.5) { [unowned self] in self.overKeyboardViewBottomConstraint.constant = 0 self.view.layoutIfNeeded() } }

  • Then your camera IBAction 然后你的相机IBAction

    @IBAction func cameraAction(_ sender: Any) { let photos = PHPhotoLibrary.authorizationStatus() switch photos { case .notDetermined: print("not determined") PHPhotoLibrary.requestAuthorization({status in if status == .authorized{ self.showGallery() } else { print("access denied") } }) case .authorized: print("authorized") self.showGallery() case .denied: print("denied") default: break } }

  • Finally your Gallery Function 最后你的画廊功能

    func showGallery() { if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { let imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = .photoLibrary; imagePicker.allowsEditing = true self.present(imagePicker, animated: true, completion: nil) } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM