在文本字段中输入文本时键盘隐藏在中间

keyboard hides in between while entering text into textfield

我正在处理包含滚动视图的登录屏幕,并且在滚动视图上有两个带有登录按钮的文本字段。

滚动视图用于调整 iphone 5 屏幕尺寸。我正在使用 "tab gesture" 所以如果任何用户在文本字段中输入文本并想要隐藏键盘然后可以单击屏幕上的任意位置以隐藏键盘。用于 Tab 手势的函数是

- (void)viewDidLoad {

NSLog(@"login view");

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[self.scrollView addGestureRecognizer:singleTap]; }

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
[self.view endEditing:YES]; }

我的问题是,当用户使用键盘在文本字段中输入文本时,然后在输入文本的过程中,键盘会检测到 Tab 手势并在中间隐藏键盘。

我做了什么来解决这个问题:- 1.) 我更改了 [self.view addGestureRecognizer:singleTap];

2.) 我在屏幕顶部放置了一个尺寸为 (0,0,360,400) 的视图并将手势应用于此视图,以便单击此视图将隐藏键盘,但当用户键入键盘时仍会通过调用隐藏手势法

3.) 我还在一半屏幕大小的滚动视图上使用了一个按钮,这样用户可以点击任何地方来隐藏键盘,但是仍然在打字时键盘隐藏了 y 调用 ibaction 方法按钮放置

使用这个来隐藏键盘和显示键盘:-

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == username)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

    }
    if (textField == password)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];

    }

    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField == username)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
    }

    if (textField == password)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidShowNotification object:nil];

    }
    return YES;
}


- (void)keyboardWillShow:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    float newVerticalPosition = -keyboardSize.height + 100;

    [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}


- (void)keyboardWillHide:(NSNotification *)notification
{
    CGFloat  kNavBarHeight =  self.navigationController.navigationBar.frame.size.height;
    [self moveFrameToVerticalPosition:kNavBarHeight forDuration:0.3f];
}

- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration
{
    CGRect frame = self.view.frame;
    frame.origin.y = position;

    [UIView animateWithDuration:duration animations:^{
        self.view.frame = frame;
    }];
}

删除 Tapgesture 并尝试此代码,它会有所帮助。

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];
}

这样试试,

 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

 [textField1 resignFirstResponder];
 [textField2 resignFirstResponder];
}   

希望这会有所帮助:)

我找到了最佳解决方案。首先,您集成 pod 'TPKeyboardAvoiding','~>1.2.3' 并添加 TPKeyboardAvoidingScrollView Class。它将处理 all.don 而不必编写额外的代码。