使用键盘

时间:2016-07-18 02:19:22

标签: ios objective-c

我的布局如下图所示:

enter image description here

我有这个代码在键盘弹出时移动Email Address文本字段:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationBeginsFromCurrentState:YES];
    textfield1.frame = CGRectMake(textfield1.frame.origin.x, (textfield1.frame.origin.y - 100.0), textfield1.frame.size.width, textfield1.frame.size.height);
            [UIView commitAnimations];
}

唯一的问题是,当我向上移动文本字段时,它会覆盖正上方的Email Login / Sign Up标签。有没有办法在不覆盖标签的情况下移动Email Address文本字段?或者我是否必须移动高于Email Address文本字段的每个视图(例如标签)?

4 个答案:

答案 0 :(得分:2)

考虑观察系统通知:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

然后查看documentation

您应该移动包装所有UITextField的视图(如果没有这样的视图,只需创建包含这些字段的“表单视图”)。通知对象的userInfo属性包含键盘的大小。您可以使用该信息并将表单视图与键盘框架保持一定距离。

答案 1 :(得分:1)

首先您需要在viewDidLoad或任何其他初始化方法中添加键盘通知观察者“UIKeyboardWillShowNotification”:

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

然后在视图控制器类中将方法“keyboardWillShow:”定义为:

- (void)keyboardWillShow:(NSNotification*)notification
{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
   CGFloat _currentKeyboardHeight = kbSize.height;
}

现在您将获得键盘高度值。根据此值,您可以调整文本字段的“y”位置。

答案 2 :(得分:1)

作为上述答案的替代方案,您是否考虑使用 UIAlertController ?这些自动格式是您希望它们的位置,以便用户可以随时输入没有丑陋的东西(好吧,并非总是,但大部分时间)。您可以阅读有关实现here的更多信息,但一般要点如下:

UIAlertController* passAlert = [UIAlertController alertControllerWithTitle:@"Email Login/Sign Up" message:@"" preferredStyle:UIAlertControllerStyleAlert];

[passAlert addTextFieldWithConfigurationHandler:^(UITextField* textField) {
        textField.placeholder = NSLocalizedString(@"username", @"username");
    }];
[passAlert addTextFieldWithConfigurationHandler:^(UITextField* textField) {
        textField.placeholder = NSLocalizedString(@"password", @"password");
    }];
UIAlertAction* doneAction = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action) {
        // do whatever you need to do to process the login and password
    }];

[passAlert addAction:doneAction];

为了透明,我对警报中文本字段的深刻智慧和晦涩的知识来自here

很有可能给出的其他答案对你的情况更有用 - 你有一个漂亮的用户界面,一种类似谷歌卡类型的东西,我打赌你不想让那个病人看起来。但是,如果你想要一个很酷的选择,那么这个应该比大多数人更加一致地工作。'临时解决方案:)。

答案 3 :(得分:0)

将scrollview作为子视图添加到现有视图,然后将所有控件放在此子视图上。

使用以下代码在文本编辑时平滑键盘推送。

//Declare in implementation file

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;

//static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;


#pragma - UITextField Delegates
- (void)textFieldDidBeginEditing:(UITextField *)textField {

    //Perform Animation during Keyboard show and Hide
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;

    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0) {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0) {
        heightFraction = 1.0;
    }

    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
} 

请参阅此link.