【问题标题】:View not moving up for custom keyboard height - swift视图不向上移动自定义键盘高度 - swift
【发布时间】:2017-08-22 15:36:23
【问题描述】:

我查看了 stackoverflow,但未能找到解决此问题的方法。我添加了代码来根据键盘的高度向上移动我的视图。这非常适用于 iOS 默认键盘,但是,这不适用于自定义键盘。这是我的代码:

import UIKit

class AddCategoryViewController: UIViewController {

var partialView: CGFloat {
    return UIScreen.main.bounds.height - 150
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.white

}

func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == partialView {
            let offset: CGSize = ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!

            if keyboardSize.height == offset.height {
                UIView.animate(withDuration: 0.1, animations: { () -> Void in
                    self.view.frame.origin.y -= keyboardSize.height
                })
            } else {
                UIView.animate(withDuration: 0.1, animations: { () -> Void in
                    self.view.frame.origin.y += keyboardSize.height - offset.height
                })
            }
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != partialView {
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.2, options: [.allowUserInteraction], animations: {
        let frame = self.view.frame
        self.view.frame = CGRect(x: 0, y: self.partialView, width: frame.width, height: frame.height)

    }, completion: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(AddCategoryViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(AddCategoryViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
}

希望您能帮我解决这个问题,以便视图将自定义键盘的高度推高。

【问题讨论】:

  • 尝试将UIKeyboardFrameBeginUserInfoKey 更改为UIKeyboardFrameEndUserInfoKey
  • @Tj3n - 这似乎不起作用。您还有其他建议吗?

标签: ios swift keyboard


【解决方案1】:

不要更改视图的框架.. 只需更改视图的翻译

   UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.2, options: [.allowUserInteraction], animations: {
    self.view.transform = CGAffineTransform(translationX : 0 , y : partialView)

}, completion: nil)

要重置它只需使用

self.view.transform = CGAffineTransform.identity

【讨论】:

  • 这不适用于非系统键盘。例如,SwiftKey 键盘在这方面表现得很奇怪。
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 2018-05-24
  • 2019-04-09
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
相关资源
最近更新 更多