我有以下问题:我有一个
scrollView
,里面有一个 textField
。我需要当我点击 textField
时,键盘会显示出来,并且 textField
(在滚动条内)立即向上移动(同时)键盘显示。现在 textField
出现,但有延迟,就像键盘显示后几毫秒,如下所示:
更新:
我已经重写了键盘处理方法,如下所示:
override func keyboardWillShow(_ notification: Notification!) {
guard let userInfo = notification.userInfo, let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
// this constraint -> viewContainerButtonBottomConstraint
//refers to the button that must have above the keyboard
//when keyboard shows up.
self.viewContainerButtonBottomConstraint.constant = frame.height
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
let contentInset = UIEdgeInsets(top: 0, left: 0, bottom: frame.height, right: 0)
scrollView.contentInset = contentInset
if #available(iOS 11.0, *) {
self.viewContainerButtonBottomConstraint.constant -= self.view.safeAreaInsets.bottom
}
}
override func keyboardWillHide(_ notification: Notification!) {
scrollView.contentInset = UIEdgeInsets.zero
self.viewContainerButtonBottomConstraint.constant = 0
UIView.animate(withDuration: 1, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
观察者的注册是在父类中进行的:
- (void)addNotificationKeyboard {
if (kKeyboardNotificationsShowAvailable) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShowNotification:)
name:UIKeyboardWillShowNotification
object:nil];
}
if (kKeyboardNotificationsHideAvailable) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHideNotification:)
name:UIKeyboardWillHideNotification
object:nil];
}
}
方法keyboardWillShowNotification和keyboardWillHideNotification也在父类中,它们被重写,就像我上面展示的那样:
- (void)keyboardWillShowNotification:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo
valueForKey:UIKeyboardFrameEndUserInfoKey];
self.keyboardHeight = [keyboardFrameBegin CGRectValue].size.height;
}
- (void)keyboardWillHideNotification:(NSNotification*)notification
{
if (self.view.frame.origin.y != 0) {
if (self.tabBarController) {
if (!(self.view.frame.origin.y == 64))
[UIUtilities moveView:self.view newYposition:-64];
}
else
[UIUtilities moveView:self.view newYposition:0];
}
[self.view endEditing:YES];
}
提前感谢您的回答!
你想要的并不难做到。在显示键盘之前会发送一个名为
UIKeyboardWillShow
的系统通知。注意这一点。它的用户信息包含有关键盘在屏幕上时的大小和位置的所有信息,以及用于将键盘移至屏幕上的动画。然后您所要做的就是立即启动匹配的动画来移动或调整您的显示。两个动画将同时运行。
这就是我所拥有的:
@objc func adjustForKeyboard(notification: Notification)
{
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide
{
self.scrollForm.contentInset = UIEdgeInsets.zero
}
else
{
self.scrollForm.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
self.scrollForm.scrollIndicatorInsets = self.scrollForm.contentInset
//let selectedRange = self.scrollForm.selectedRange
//self.scrollForm.scrollRangeToVisible(selectedRange)
}
并且在
viewDidLoad()
内:
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillHide, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
这可能与您所拥有的类似,请尝试用上面的内容替换您的覆盖。