iphone - 向上移动 View ,使文本字段不会消失在键盘后面

标签 iphone ios objective-c uiview keyboard-events

我有一个像你在这里看到的景色。 enter image description here

您可以看到我有一个标题图像,下面有很多文本字段。仅 UITextfields 地址 - 邮政编码 - Gemeente - Tel.nr - 电子邮件 - BTWnr。消失在键盘后面。 我有一些代码,适用于 Adres 的 UITextfield。但当我想实现更多功能时,它总是需要 Adres 的 UITextfield 的动画。

这是我的代码

#define kOFFSET_FOR_KEYBOARD 80.0
#define kOFFSET_FOR_KEYBOARD2 120.0
-(void)keyboardWillShow {

    NSLog(@"Keyboard frame now is %f",self.keyboardView.frame.origin.y);
    // Animate the current view out of the way
    if (self.keyboardView.frame.origin.y >= 198)
    {
        NSLog(@"keyboardWillShow 1");
        [self setViewMovedUp2:NO];
    }
    else if (self.keyboardView.frame.origin.y < 198)
    {
         NSLog(@"keyboardWillShow 2");
        [self setViewMovedUp:YES];
    }
}

-(void)keyboardWillHide {

    NSLog(@"Keyboard frame  ATM %f",self.keyboardView.frame.origin.y);
    if (self.keyboardView.frame.origin.y >= 198)
    {
        NSLog(@"keyboardWillHide 1");
        [self setViewMovedUp:YES];
    }
    else if (self.keyboardView.frame.origin.y < 198)
    {
        NSLog(@"keyboardWillHide 2");
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{

    if ([sender isEqual:txtAdres])
    {
        NSLog(@"sender is adres");
        //move the main view, so that the keyboard does not hide it.
        if  (self.keyboardView.frame.origin.y >= 198)
        {
            [self setViewMovedUp:YES];
        }
    }
    if ([sender isEqual:txtPostcode])
    {
        NSLog(@"sender is postcode");
        //move the main view, so that the keyboard does not hide it.
        if  (self.keyboardView.frame.origin.y >= 198)
        {
            [self setViewMovedUp2:YES];
        }
    }
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.keyboardView.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.keyboardView.frame = rect;

    [UIView commitAnimations];
}
-(void)setViewMovedUp2:(BOOL)movedUp
{
    NSLog(@"setViewMovedUp2 called");
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.keyboardView.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD2;
        rect.size.height += kOFFSET_FOR_KEYBOARD2;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD2;
        rect.size.height -= kOFFSET_FOR_KEYBOARD2;
    }
    self.keyboardView.frame = rect;

    [UIView commitAnimations];
}

在我的 ViewWillAppear 中我这样做

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

有人可以帮我解决这个问题吗?

最佳答案

请尝试使用这个...我希望它可以帮助你。您可以在这一行您想要文本字段的地方将值设置为 200。

 CGFloat avaliableHeight = applicationFrame.size.height - 200;


- (void)scrollViewToCenterOfScreen:(UIView *)theView
{
    CGFloat viewCenterY = theView.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGFloat avaliableHeight = applicationFrame.size.height - 200;

    CGFloat y = viewCenterY - avaliableHeight / 2.0f;

    if (y < 0)
    {
        y = 0;
    }

    [scrollView setContentOffset:CGPointMake(0, y) animated:YES];
}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self scrollViewToCenterOfScreen:textField];
    return YES;

}

关于iphone - 向上移动 View ,使文本字段不会消失在键盘后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16440512/

相关文章:

iphone - 为什么窗口自动释放在应用程序 :didFinishLaunchingWithOptions: and released in dealloc?

ios - Xcode 将屏幕分成 4 个大按钮

ios - 一起使用 Novocaine 和 AVCam

ios - 何时在应用程序 :performFetchWithCompletionHandler: when Background Fetch is async? 中调用 completionHandler

ios - 在 iOS7 UITabbarController 中,如何停止在点击已选择的 tabBarItem 时将 navigationController 重置为 root?

java - 无法在 Java 中验证在 iOS 上签名的数据

iphone - 在托管对象上下文之间传递对象

ios - 在 ios 6 和 ios 7 中未收到推送通知

ios - drawPageAtIndex 在 iOS 8.1.1 中挂起

ios - 动态更改由自动布局控制的 View 的纵横比