UITextField-当应用程序来自后台时键盘不会隐藏 [英] UITextField - Keyboard not hiding when app comes from background

查看:84
本文介绍了UITextField-当应用程序来自后台时键盘不会隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用中,我的UITextfield位于视图底部.因此,当用户开始输入文本时,我会将视图向上滑动,以便用户可以看到他们正在输入的内容.
输入文本时,视图会向上移动,然后按主屏幕按钮,该应用程序将进入背景.
现在点击应用程序的图标,它将应用程序置于前景
现在我注意到视图返回到原始位置(X = 0,Y = 0),但是键盘仍然可见.
当应用程序进入前台时如何隐藏键盘.

In my iOS app, I've UITextfield at the bottom of the view. So, when user starts entering text, I'm sliding the view up so that users can see what they are typing.
While entering text, view moves upwards, then press home button and the app goes to background.
Now tap on the icon of the app and it brings the app to foreground
Now I noticed that view comes back to original position (X=0, Y=0) but keyboard is still visible.
How to hide the keyboard when the app comes to foreground.

我试图将键盘隐藏在viewWillAppear和viewWillDisappear中.没用.

I tried to hide the keyboard in viewWillAppear and viewWillDisappear. It didn't work.

推荐答案

 -(void)applicationDidEnterBackground:(UIApplication *)application
  {
       [self.window endEditing:YES];
  }

**********或************

********** OR ************

当您来自后台时,您必须致电通知.当您从背景输入到前景时,则调用此appdelegate方法.

you have to call Notification when you come from background. when you enter from background to foreground then calling this method of appdelegate.

- (void)applicationWillEnterForeground:(UIApplication *)application

按如下所示触发其中的通知.

fire the notification in it as below.

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDE" object:self];
}

如下所示在您的视图控制器中添加通知

Add notification in your viewcontroller as below

-(IBAction)HideKeyBard:(NSNotification *)noty
{
    [txt resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HideKeyBard:) name:@"HIDE" object:nil];
}

输出:

这篇关于UITextField-当应用程序来自后台时键盘不会隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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