如何偏移文本视图等于键盘在swift 3中的高度?
我在我的项目中有一个文本视图,我想当用户点击文本字段时,它向上移动等于键盘的高度我使用此代码获取键盘的高度,它给我的键盘大小如何偏移文本视图等于键盘在swift 3中的高度?
static var sizeForOffsetKeyboard = CGFloat()
var heightKeyboard : CGFloat?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
func keyboardShown(notification: NSNotification) {
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
levelChatViewController.sizeForOffsetKeyboard = heightKeyboard!
print(levelChatViewController.sizeForOffsetKeyboard)
// Now is stored in your heightKeyboard variable
}
}
但我没有得到的结果和TextView中不会与键盘
所以这里的高度拉升是偏移的TextView代码
func textViewDidBeginEditing(_ textView: UITextView) {
animateViewMoving(up: true, moveValue: levelChatViewController.sizeForOffsetKeyboard)
}
最简单的方法是使用IQKeyboardManagerSwi FT
添加荚
pod 'IQKeyboardManagerSwift'
在你的AppDelegate
import IQKeyboardManagerSwift
内didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
IQKeyboardManager.sharedManager().enable = true
}
这将管理所有的文本提交你的项目,你不必写其他地方的任何东西
刚刚尝试了一次,你会爱上它
我不想使用POD文件,因为我的项目的规模是非常巨大的 –
745 KB大小只有 –
我已阅读,感谢帮助但我需要一些简单的东西,所以我决定让尺寸手册感谢您的帮助 –
可以使用的UITableViewController自动滚动。检查我的答案在这里https://*.com/questions/45233089/move-textfield-up-when-keyboard-displays/45233564#45233564 – luckyShubhra
我用https://*.com/questions/26070242/move-view- with-keyboard-using-swift and its ok但是,当文本字段正在编辑并且用户更改键盘时,文本字段将不会随新键盘移动 –
当基于当前选择哪个文本文件的键盘时,您必须继续添加高度。你可以使用UITableViewController,你可以用零代码实现你想要的东西。 – luckyShubhra