在iOS 8上显示键盘时停用视图向上移动

时间:2014-12-15 09:29:43

标签: ios ios8

在我的应用程序中,我有一个注册表单,有一些textFields(电子邮件,密码等...),当我开始在这些textFields中键入某个地方(显示键盘)时,我的视图向上移动,而我&# 39;我无法看到在我的文本字段中键入的内容。

只有iOS8才会出现此问题。

如果有人知道如何阻止这个操作系统功能,我会感激不尽。

的问候。

2 个答案:

答案 0 :(得分:1)

以下是我的代码,用于管理与键盘外观和消失相关的问题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self registerForKeyboardNotifications];
} 

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

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

}

#pragma mark - Keyboard Delegate
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    //to overcome the bug with keyboard height for os7 and earlier
    if (![UIDeviceHardware isOS8Device]) {
        CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        kbRect = [self.view convertRect:kbRect fromView:nil];
        kbSize = kbRect.size;
    }

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [_scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
}

您可以从activeField选择UITextfield Delegate

答案 1 :(得分:0)

这对你有用..

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

    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y - 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField becomeFirstResponder];
    }

}

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

    if (textField == (UITextField *)[self.view viewWithTag:TEXTFIELD_MOBILE_TAG])
    {
        CGRect selfframe = CGRectMake([self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.x, [self.view viewWithTag:VIEW_PARENT_TAG].frame.origin.y + 80, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.width, [self.view viewWithTag:VIEW_PARENT_TAG].frame.size.height);
        [UIView animateWithDuration:0.3 animations:^{ [self.view viewWithTag:VIEW_PARENT_TAG].frame = selfframe;}];
        [textField resignFirstResponder];
    }

}