iOS/Xcode:出现键盘时如何向上移动视图 [英] IOS/Xcode: How to Move View Up when Keyboard appears

查看:197
本文介绍了iOS/Xcode:出现键盘时如何向上移动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SO的答案很多,但是大多数答案是几年前的,而且似乎还不太令人满意.

There are many answers on this on SO but most are from years ago and none seem quite satisfactory.

对于只有三个文本字段和提交按钮的登录屏幕,我希望视图在键盘刚好出现时向上移动,以便在不隐藏该字段的情况下也不会向上移动视图.所需的移动量应使提交"按钮在键盘上方具有固定距离.虽然可以通过向上移动页面上的字段来为键盘留出空间,但是提交"按钮仍处于隐藏状态

For a login screen with just three textfields and submit button, I want the view to move up when the keyboard appears just enough so that while the field is not hidden, it also does not move up out of view. The amount of movement needed is such that the submit button is a fixed distance above the keyboard. While it is possible by moving the fields high up on the page to leave room for the keyboard, the submit button is still hidden

我尝试仅添加以下内容:

I tried just adding the following:

-(void) viewWillAppear:(BOOL)Animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}
- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

这会将视图向上移动固定的数量,但幅度太大,以至于字段对于编辑而言是不可见的,即它们太高了.

This moves the view up a fixed amount but so much so that the fields are not visible for editing, i.e. they are too high up.

另一个SO答案建议:

-(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 = -200; // 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];
}

我不知道该如何实现.如果您只是将其保留,则什么也不会发生.我猜您应该使用文本字段的名称来重命名文本字段,但是在那种情况下,您会为每个文本字段都这样做吗?我无法使它产生任何效果.

I can't figure out how to implement this. If you just leave it as is nothing happens. I guess you are supposed to rename textfield with the name of your textfield but in that case, would you do it for each of the textfields? I cannot get it to have any effect.

另一个建议是使用诸如TPKeyboardAvoiding之类的类别,但这需要在这种情况下不需要的滚动视图.

Another suggestion is to use a category such as TPKeyboardAvoiding however this requires a scrollview that I do not need in this case.

2015年是否没有针对此问题的简单解决方案?

Is there no straightforward solution in 2015 for this issue?

感谢您的任何建议/指导.

Thanks for any suggestions/guidance.

推荐答案

当用户开始键入内容时,以下动画会将您的视图(在这种情况下为viewForLogin)移动200像素以上.文本字段结束编辑后,视图将恢复为原始位置的动画.不要忘记为文本字段设置委托.

The following animation will move your view (viewForLogin in this case) 200 pixels above when the user starts typing. The view will animate back to original position when the textfield ends editing. Do not forget to set the delegates for the textfields.

快捷键3

func textFieldDidBeginEditing(_ textField: UITextField) {
    UIView.animate(withDuration: 0.3, animations: {
        self.view.frame = CGRect(x:self.view.frame.origin.x, y:self.view.frame.origin.y - 200, width:self.view.frame.size.width, height:self.view.frame.size.height);

    })
}

func textFieldDidEndEditing(_ textField: UITextField) {
    UIView.animate(withDuration: 0.3, animations: {
        self.viewSupport.frame = CGRect(x:self.viewSupport.frame.origin.x, y:self.viewSupport.frame.origin.y + 200, width:self.viewSupport.frame.size.width, height:self.viewSupport.frame.size.height);

    })
}

Objective-C

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationBeginsFromCurrentState:TRUE];
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200., self.view.frame.size.width, self.view.frame.size.height);

    [UIView commitAnimations];


}


-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    [UIView setAnimationBeginsFromCurrentState:TRUE];
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +200., self.view.frame.size.width, self.view.frame.size.height);

    [UIView commitAnimations];

}

这篇关于iOS/Xcode:出现键盘时如何向上移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆