【问题标题】:keyboard hides in between while entering text into textfield将文本输入文本字段时,键盘隐藏在两者之间
【发布时间】:2016-04-25 21:54:16
【问题描述】:

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

用于调整 iphone 5 屏幕尺寸的滚动视图。我正在使用“标签手势”,因此如果任何用户在文本字段中输入文本并想要隐藏键盘,则可以单击屏幕上的任意位置以隐藏键盘。用于标签手势的函数是

- (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 方法强>

【问题讨论】:

    标签: ios objective-c keyboard uitapgesturerecognizer first-responder


    【解决方案1】:

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

    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
        [self.view endEditing:YES];
    }
    

    【讨论】:

    • 我实现了它正在工作的方法,但是返回后它在顶部给出了黑色空间
    • 检查你为点击手势放置的视图并检查滚动视图。
    【解决方案2】:

    使用它来隐藏键盘并显示键盘:-

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

    【讨论】:

    • 感谢您的回复...我已经实现了您提供的相同代码...但它没有解决我的问题...我的问题是在键入键盘时通过调用手势方法自动隐藏.
    • 评论说只有我在这里发布的这个方法在你的项目中使用它会帮助你......
    • 好的......谢谢我会评论标签手势......但在那之后点击视图不会关闭键盘......我猜?
    • 点击返回键键盘会隐藏
    • 我删除了滚动视图和点击手势并实现了 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event----方法,现在它可以工作了完美
    【解决方案3】:

    我找到了最好的解决方案。首先,您集成 pod 'TPKeyboardAvoiding'、'~>1.2.3' 并添加 TPKeyboardAvoidingScrollView 类。它将处理所有。不必编写额外的代码。

    【讨论】:

      【解决方案4】:

      试试这样,

       -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
      
       [textField1 resignFirstResponder];
       [textField2 resignFirstResponder];
      }   
      

      希望这会有所帮助:)

      【讨论】:

      • 感谢您的响应....实际上我在视图上使用了滚动视图....所以在我删除滚动视图并且触摸仅在自己的视图上检测到之前,触摸方法将不起作用。 ...
      • 然后在该滚动视图上添加视图,然后将文本字段添加到该视图
      • 因为我定制了整个功能,比如使用滚动视图根据屏幕大小移动文本字段......我刚刚删除了标签手势和滚动视图......我使用 @aayush ans 来自动调整键盘出现时的视图并使用 touchesBegan 方法处理触摸并隐藏键盘....再次感谢
      • 不客气...:) 这不是标签手势,而是tap gesture!!!我发现你到处都在使用标签手势!!
      猜你喜欢
      • 2014-03-18
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多