[英]UiTextfield does not move up when Keyboard pops up ios 11 (Working on Previous versions)
問題:在ios 11中不起作用(但是在ios 8中工作)
以下代碼是用Swift 2.0編寫的。 但是我的應用程序太大,無法一次遷移代碼並發布更新。
目的:我想使用xcode 7發布版本,但是在ios 11上進行調試時卻得到了“開發人員磁盤映像”。因此,如何在不遷移代碼的情況下修復錯誤
func viewDidLoad(){
super.baseScrolllView = scrollView
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillHideNotification, object: nil);
}
基類中的代碼
var keyboardIsVisible = false
var baseScrolllView: UIScrollView!
func keyboardAdjust(notification: NSNotification) {
let info = notification.userInfo!
let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)
if notification.name == UIKeyboardWillShowNotification && keyboardIsVisible == false{
keyboardIsVisible = true
UIView.animateWithDuration(duration, animations: { ()
var contentInset:UIEdgeInsets = self.baseScrolllView.contentInset
contentInset.bottom = keyboardFrame.size.height
self.baseScrolllView.contentInset = contentInset
})
}else {
keyboardIsVisible = false
UIView.animateWithDuration(duration, animations: { ()
var contentInset:UIEdgeInsets = UIEdgeInsetsZero
self.baseScrolllView.contentInset = contentInset
})
}
}
這就是我得到鍵盤高度的方法
@objc func keyboardWillShow(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }
containerViewBottomConstraint.constant = keyboardSize.height
UIView.animate(withDuration: duration) {
self.containerView.layoutIfNeeded()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.