我有一个带有一些自定义单元格的UITable,在最后一个单元格中我有一个UITextField
,我希望打开键盘时所有单元格都会向上滚动(所有类型的键盘都带有"预测"启用和禁用)。
我已经查看了有关此主题的其他问题并尝试了但它仍然不够好(键盘的单元格之间存在差距,tableView的动画和键盘都没有&# 39; t同步等...)。
你能帮帮我吗?
谢谢!
答案 0 :(得分:2)
查看Github上的iOS Firechat示例项目。它使用tableView外部的文本字段,并在键盘出现/消失时将其与键盘一起移动。
特别是本节:
// Setup keyboard handlers to slide the view containing the table view and
// text field upwards when the keyboard shows, and downwards when it hides.
- (void)keyboardWillShow:(NSNotification*)notification
{
[self moveView:[notification userInfo] up:YES];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
[self moveView:[notification userInfo] up:NO];
}
- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up
{
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]
getValue:&keyboardEndFrame];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]
getValue:&animationCurve];
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
getValue:&animationDuration];
// Get the correct keyboard size to we slide the right amount.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
int y = keyboardFrame.size.height * (up ? -1 : 1);
self.view.frame = CGRectOffset(self.view.frame, 0, y);
[UIView commitAnimations];
}
这里的Swift相同,未经测试:
func moveView(userInfo : NSDictionary, up : Bool){
var keyboardEndFrame : CGRect?
userInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)?.getValue(&keyboardEndFrame)
var animationCurve : UIViewAnimationCurve?
userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey)?.getValue(&animationCurve)
var animationDuration : NSTimeInterval?
userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey)?.getValue(&animationDuration)
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(animationDuration!)
UIView.setAnimationCurve(animationCurve!)
let keyboardFrame = self.view.convertRect(keyboardEndFrame!, toView: nil)
let y = keyboardFrame.size.height * (up ? -1 : 1);
self.view.frame = CGRectOffset(self.view.frame, 0, y);
UIView.commitAnimations()
}