我试图在iOS 8中的键盘上方保留一个文本字段。但是,当用户在键盘顶部上下滑动以显示或拒绝iOS 8单词建议时,我需要通知键盘的新高度,以便在该高度上下移动文本字段。
我怎样才能做到这一点?
谢谢!
发布于 2015-05-02 02:32:58
您可以注册UIKeyboardDidShowNotification,然后使用UIKeyboardFrameEndUserInfoKey从通知中获取键盘帧。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleKeyboardDidShowNotification:)
name:UIKeyboardDidShowNotification
object:nil];
- (void)handleKeyboardDidShowNotification:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// Move your textview into view here
}即使在键盘已经显示并且只是改变大小的情况下,也会发送此通知,因此每当您在键盘顶部上下滑动时,都会收到通知。
发布于 2015-05-02 02:33:50
下面是我构建的自定义inputView中的一些代码。它甚至为您的自定义视图处理动画曲线,以便它与键盘的速度相匹配并随其移动。
当建议被显示/隐藏时,通知将被触发。
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil]; }
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
CGSize endKbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect baseFrame = self.frame;
baseFrame.origin.y = CGRectGetMaxY(self.frame) - (endKbSize.height - 5);
[UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
self.frame = baseFrame;
} completion:^(BOOL finished) {
self.frame = baseFrame;
}]; }
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
[UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
self.frame = _originalRect;
} completion:nil];
}发布于 2018-06-21 12:47:46
Swift 4解决方案:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
registerKeyboardNotifications()
}
func registerKeyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow(notification:)),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide(notification:)),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc func keyboardWillShow(notification: NSNotification) {
let userInfo: NSDictionary = notification.userInfo! as NSDictionary
let keyboardInfo = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
let keyboardSize = keyboardInfo.cgRectValue.size
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
scrollView.contentInset = contentInsets
scrollView.scrollIndicatorInsets = contentInsets
}
@objc func keyboardWillHide(notification: NSNotification) {
scrollView.contentInset = .zero
scrollView.scrollIndicatorInsets = .zero
}https://stackoverflow.com/questions/29997618
复制相似问题