gpt4 book ai didi

ios - 查看不向上移动自定义键盘高度 - swift

转载 作者:行者123 更新时间:2023-11-28 06:13:25 25 4
gpt4 key购买 nike

我查看了 stackoverflow,但未能找到解决此问题的方法。我添加了代码以根据键盘的高度向上移动我的 View 。这非常适合 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)
}
}

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

最佳答案

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

   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)

And to Reset it just use

self.view.transform = CGAffineTransform.identity

关于ios - 查看不向上移动自定义键盘高度 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com