我有一个UIView,它不在UIScrollView中.我想在键盘出现时向上移动我的视图.在我尝试使用此解决方案之前:如何在键盘存在时使UITextField向上移动?.
它工作正常.但是在文本字段中插入数据后,它会转到另一个视图,当我回到这个页面时,它只是跳跃,我看不到我的文本字段.有没有更好的解决方案来解决这个问题?
切换到iOS8后,当我在键盘转换期间移动视图时,我会遇到奇怪的行为.谁能解释一下发生了什么?
这是一个演示问题的最小例子.我有一个简单的视图与a UITextField和a UIButton.该功能nudgeUp将文本字段和按钮向上移动10个点.它由buttonPressed回调或keyboardWillShow回调触发.
当我点击按钮时,代码按预期工作:buttonPressed调用nudgeUp和按钮和文本字段跳起10点.
当我点击文本字段,keyboardWillShow调用nudgeUp,但行为是非常不同的.按钮和文本字段立即向下跳过10个点,然后在键盘显示时自动向后滑动到原始位置.
为什么会这样?如何在iOS8键盘演示期间重新获得对动画的控制?
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Called when the keyboard appears.
[self nudgeUp];
}
- (IBAction)buttonPressed:(id)sender {
[self nudgeUp];
}
- (void)nudgeUp
{
CGRect newTextFieldFrame = self.textField.frame;
newTextFieldFrame.origin.y -= 10;
self.textField.frame = newTextFieldFrame;
CGRect newButtonFrame = self.button.frame;
newButtonFrame.origin.y -= 10; …Run Code Online (Sandbox Code Playgroud)