gpt4 book ai didi

ios - 在 iOS 中被键盘隐藏的 TextField

转载 作者:行者123 更新时间:2023-11-29 12:57:20 29 4
gpt4 key购买 nike

我有一个调出键盘的文本框。键盘隐藏了文本字段的底部。我已将 textField 放在 ScrollView 中,并在 View 中编写了以下代码。

- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
// Called when the keyboard is shown
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_driverSignupScrollView.contentInset = contentInsets;
_driverSignupScrollView.scrollIndicatorInsets = contentInsets;

// If active field is hidden by keyboard, scroll it so it's visible
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;

CGPoint activeFieldPoint = CGPointMake(_activeField.frame.origin.x, _activeField.frame.origin.y + _activeField.frame.size.height);
if (!CGRectContainsPoint(aRect, activeFieldPoint)) {
[_scrollView scrollRectToVisible:_activeField.frame animated:YES];
}
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification {
// Called when the keyboard is hidden
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}

但是控件永远不会进入我的 if (!CGRectContainsPoint(aRect, activeFieldPoint)) 因此它不会向上滚动。

正在另一种方法中正确设置 _activeField

我在这里遗漏了什么吗?

最佳答案

看看 TPKeyboardAvoiding:https://github.com/michaeltyson/TPKeyboardAvoiding

它使键盘管理变得异常简单。这就是 Apple 应该从一开始就设计它的方式。

关于ios - 在 iOS 中被键盘隐藏的 TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708899/

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