4 回答
TA贡献1804条经验 获得超3个赞
实际上,最好只使用Apple的实现,如文档中所提供的那样。但是,他们提供的代码是错误的。将keyboardWasShown:
评论下方的部分替换为以下内容:
NSDictionary* info = [aNotification userInfo];
CGRect keyPadFrame=[[UIApplication sharedApplication].keyWindow convertRect:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] fromView:self.view];
CGSize kbSize =keyPadFrame.size;
CGRect activeRect=[self.view convertRect:activeField.frame fromView:activeField.superview];
CGRect aRect = self.view.bounds;
aRect.size.height -= (kbSize.height);
CGPoint origin = activeRect.origin;
origin.y -= backScrollView.contentOffset.y;
if (!CGRectContainsPoint(aRect, origin)) {
CGPoint scrollPoint = CGPointMake(0.0,CGRectGetMaxY(activeRect)-(aRect.size.height));
[backScrollView setContentOffset:scrollPoint animated:YES];
}
Apple代码的问题是这些:(1)它们总是计算点是否在视图的框架内,但它是a ScrollView,所以它可能已经滚动,你需要考虑该偏移:
origin.y -= scrollView.contentOffset.y
(2)他们将contentOffset移动键盘的高度,但我们想要相反(我们想要移动contentOffset屏幕上可见的高度,而不是不是):
activeField.frame.origin.y-(aRect.size.height)
TA贡献1898条经验 获得超8个赞
在textFieldDidBeginEditting
和textFieldDidEndEditing
通话功能[self animateTextField:textField up:YES]
,如下所示:
-(void)textFieldDidBeginEditing:(UITextField *)textField { [self animateTextField:textField up:YES]; }- (void)textFieldDidEndEditing:(UITextField *)textField{ [self animateTextField:textField up:NO];}-(void)animateTextField:(UITextField*)textField up:(BOOL)up{ const int movementDistance = -130; // tweak as needed const float movementDuration = 0.3f; // tweak as needed int movement = (up ? movementDistance : -movementDistance); [UIView beginAnimations: @"animateTextField" context: nil]; [UIView setAnimationBeginsFromCurrentState: YES]; [UIView setAnimationDuration: movementDuration]; self.view.frame = CGRectOffset(self.view.frame, 0, movement); [UIView commitAnimations];}
我希望这段代码可以帮到你。
在Swift 2中
func animateTextField(textField: UITextField, up: Bool) { let movementDistance:CGFloat = -130 let movementDuration: Double = 0.3 var movement:CGFloat = 0 if up { movement = movementDistance } else { movement = -movementDistance } UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(movementDuration) self.view.frame = CGRectOffset(self.view.frame, 0, movement) UIView.commitAnimations()}func textFieldDidBeginEditing(textField: UITextField) { self.animateTextField(textField, up:true)}func textFieldDidEndEditing(textField: UITextField) { self.animateTextField(textField, up:false)}
SWIFT 3
func animateTextField(textField: UITextField, up: Bool) { let movementDistance:CGFloat = -130 let movementDuration: Double = 0.3 var movement:CGFloat = 0 if up { movement = movementDistance } else { movement = -movementDistance } UIView.beginAnimations("animateTextField", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(movementDuration) self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement) UIView.commitAnimations() } func textFieldDidBeginEditing(textField: UITextField) { self.animateTextField(textField: textField, up:true) } func textFieldDidEndEditing(textField: UITextField) { self.animateTextField(textField: textField, up:false) }
- 4 回答
- 0 关注
- 674 浏览
添加回答
举报