iOS的scrollView setContentOffset“shimmy”
我有一个带有多个文本字段的滚动视图,它跟踪活动字段并确保它在键盘弹出时可见。一切都运行良好,但是当我从第三个文本字段切换到第四个文本字段时,在文本字段最终到达正确位置之前,我会出现一点上下“晃动”。有什么建议吗?
-(void)keyboardDidShow:(NSNotification *)notification
{
if (keyboardIsShown)return;
NSDictionary* info=[notification userInfo];
// get keyboard size
CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
//Set scrollview insets to make room for keyboard
UIEdgeInsets contentInsets=UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
scrollView.contentInset=contentInsets;
scrollView.scrollIndicatorInsets=contentInsets;
//scroll the active text field into view
CGRect viewFrame=self.view.frame;
viewFrame.size.height-=keyboardSize.height;
int fieldHeight=self.currentTextField.bounds.size.height;
CGFloat navHeight=self.navigationController.navigationBar.frame.size.height;
CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
if (!CGRectContainsPoint(viewFrame, viewPoint)) {
//scroll to make sure active field is showing
CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-keyboardSize.height+navHeight);//+navHeight
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
-(void)showActiveField
{
//this makes sure that activeField shows when selecting another field after initial keyboard show
int fieldHeight=self.currentTextField.bounds.size.height;
CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
CGRect viewFrame=self.view.frame;
int inset=scrollView.contentInset.bottom;
if (!CGRectContainsPoint(viewFrame, viewPoint)) {
//scroll to make sure active field is showing
CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-inset);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
I have a scrollView with multiple textFields, which tracks the active field and makes sure it is visible when the keyboard pops up. It all works well, but when I tab from the 3rd to 4th textField, I get a little up and down "shimmy" before the textField ends up in the right place. Any suggestions?
-(void)keyboardDidShow:(NSNotification *)notification
{
if (keyboardIsShown)return;
NSDictionary* info=[notification userInfo];
// get keyboard size
CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
//Set scrollview insets to make room for keyboard
UIEdgeInsets contentInsets=UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
scrollView.contentInset=contentInsets;
scrollView.scrollIndicatorInsets=contentInsets;
//scroll the active text field into view
CGRect viewFrame=self.view.frame;
viewFrame.size.height-=keyboardSize.height;
int fieldHeight=self.currentTextField.bounds.size.height;
CGFloat navHeight=self.navigationController.navigationBar.frame.size.height;
CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
if (!CGRectContainsPoint(viewFrame, viewPoint)) {
//scroll to make sure active field is showing
CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-keyboardSize.height+navHeight);//+navHeight
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
-(void)showActiveField
{
//this makes sure that activeField shows when selecting another field after initial keyboard show
int fieldHeight=self.currentTextField.bounds.size.height;
CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight);
CGRect viewFrame=self.view.frame;
int inset=scrollView.contentInset.bottom;
if (!CGRectContainsPoint(viewFrame, viewPoint)) {
//scroll to make sure active field is showing
CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-inset);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在哪里设置
keyboardIsShown
?您不想在检查它是否已经设置后立即执行此操作吗?然后:第四个字段是否靠近滚动视图的末尾并且您设置了反弹滚动?
Where do you set
keyboardIsShown
? Don't you want to do that Right after you check if it is already set?And then: is the 4th field near the end of the scrollview and you have bounce scroll set?