当iPad虚拟键盘出现时,它会覆盖我的视图中的一些文本框。有没有办法在键盘出现时向上移动ViewController?当iPad出现键盘时如何移动视图?
0
A
回答
1
我的建议是将您的对象放在UIScrollView
上,然后当键盘出现时,您可以使用scrollRectToVisible:
。
[scroller scrollRectToVisible:frame animated:YES];
7
我已经为我的一个应用程序写了这段代码。
它会自动检测TextField的位置并相应地滚动baseView。
- (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
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait){
if(up) {
int moveUpValue = temp.y+textField.frame.size.height;
animatedDis = 264-(1024-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
if(up) {
int moveUpValue = 1004-temp.y+textField.frame.size.height;
animatedDis = 264-(1004-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft) {
if(up) {
int moveUpValue = temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
}
}
else
{
if(up) {
int moveUpValue = 768-temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
}
}
if(animatedDis>0)
{
const int movementDistance = animatedDis;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
if (orientation == UIInterfaceOrientationPortrait){
baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationLandscapeLeft) {
baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
}
else {
baseViewController.view.frame = CGRectOffset(baseViewController.view.frame, 0, movement);
}
[UIView commitAnimations];
}
}
1
时候做起来=无变化ValayPatel代码“如果”语句if(animatedDis>0 || !up)
的条件,否则转移的观点是总是在前面的位置。
+0
我认为原始代码的目的是animatedDis是一个属性,因此编辑完成后> 0值将被保留,导致视图动画到原始位置。 – Noam
相关问题
- 1. 当键盘出现时移动视图
- 2. 如何在键盘出现在内部时移动视图ConstraintLayout
- 3. 当虚拟键盘出现弹出在iPad中向上移动?
- 4. 在键盘出现时发出移动视图Swift
- 5. 键盘出现时移动视图 - 仅在编辑时?
- 6. 保持键盘上方的视图 - 当出现键盘时
- 7. 当键盘出现时,如何使CCTextFieldTTF向上移动:Cocos2d-x
- 8. 带有TextField的UITableView并在键盘出现时移动视图
- 9. Xcode:在键盘出现时移动视图
- 10. 当键盘出现时移动桌面视图的内容插入
- 11. 如何当键盘出现
- 12. 当键盘弹出时移动UIButton
- 13. 键盘出现时向上移动UITextField
- 14. 键盘出现时移动UIView
- 15. 键盘出现时移动UIView
- 16. 如何在键盘出现时使android视图滚动?
- 17. 如何在键盘出现时向上滚动视图?
- 18. 如何在目标c中出现键盘时滚动视图?
- 19. 当弹出键盘时,如何将整个视图向上移动? (Swift)
- 20. Swift:当出现键盘时滚动UITableView
- 21. 当键盘出现时向上移动UIView
- 22. 当软键盘出现时移动布局
- 23. 当键盘出现时,UIViewController向上移动
- 24. 当键盘出现时将UITextField和UITableView向上移动
- 25. 当键盘出现时,UIPopoverController在iOS 7上奇怪地移动
- 26. Swift:当键盘出现时向上移动UILabel
- 27. 当键盘出现时jQuery Mobile固定页脚正在移动
- 28. 滚动视图与滚动视图里面,当软键盘出现时,父滚动视图不滚动
- 29. 当键盘出现
- 30. 当键盘显示在UITextField上时移动视图
我在这方面很新,你有一个简单的例子,关于如何做到这一点? 谢谢! – Guerrix
谢谢,我真的很感激。 – Guerrix