当用户按下主页按钮时暂停键盘?

发布于 2025-01-06 07:02:17 字数 163 浏览 4 评论 0原文

我正在开发一个应用程序,一切正常,除了一个例外,即当用户在键盘处于活动状态时按主页并再次打开我的应用程序时,视图框架边界正在更改并移出边界。我的预期结果是键盘应该暂停,或者当键盘处于非活动状态从后台返回到前台时视图应该保持在相同的位置。

我希望人们理解我的情况并尽快回复。

谢谢。

I am developing an application were everything is working fine, except one i.e. when user press on home while keyboard is in active and again opens my application the view frame bounds are changing and moving out of bounds. My expected result is keyboard should get suspended or the view should stay in the same position when it is come back from background to foreground with keyboard in-active state.

I hope people understand my scenario and reply ASAP.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗳卜坏 2025-01-13 07:02:17

我已经找到了我的问题的解决方案,我希望人们可以使用我的解决方案。下面是我所做的代码,

在您的 RootViewController 文件中添加以下代码行(即当您打开应用程序时首先出现哪个视图)。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];

然后添加一个私有方法如下

- (void) receivedNotification:(NSNotification *) notification
{
if ([username isFirstResponder])
{
    [username resignFirstResponder];
}
else if ([password isFirstResponder])
{
    [password resignFirstResponder];
}
}

我希望它对一些人有帮助,谢谢。

进一步帮助请参阅提到的链接,

I have found the solution to my question, i hope people can use my solution. Below is the code what I have done,

Add the below line of code in your RootViewController file (i.e. which view is coming at first when you open your APP).

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];

And then add a private method as below

- (void) receivedNotification:(NSNotification *) notification
{
if ([username isFirstResponder])
{
    [username resignFirstResponder];
}
else if ([password isFirstResponder])
{
    [password resignFirstResponder];
}
}

I hope it help some body,Thank u.

Further assistance please see the mentioned link,

仅一夜美梦 2025-01-13 07:02:17

应用程序委托中有一个方法,

- (void)applicationDidEnterBackground:(UIApplication *)application

当您按下主页按钮时会触发该方法。
在此方法中进行必要的更改(textField resignFirstResponder),我想它应该可以正常工作。

代码

编辑这是类中的

-(void)performWhenHomeBtnprssed
{
[MytextField resignFirstResponder];
}

,您在其中让文本字段创建一个方法,然后

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  [myClassObj performWhenHomeBtnprssed];
}

我也同意@valexa,您应该找到问题的根本原因

there is a method in the app delegate

- (void)applicationDidEnterBackground:(UIApplication *)application

this method is fired when you press the home button.
do the necessary changes(textField resignFirstResponder) in this method and it should work fine i guess.

EDIT here's the code

in the class where you have your textfield create a method

-(void)performWhenHomeBtnprssed
{
[MytextField resignFirstResponder];
}

then in

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  [myClassObj performWhenHomeBtnprssed];
}

also i agree with @valexa you should find the root cause of the problem

傾城如夢未必闌珊 2025-01-13 07:02:17

在软件开发中,解决根本原因总是比修补效果更好,在您的情况下,视图的定位存在问题,您应该解决这个问题,前景/后台循环不应影响视图定位。

In software development it is always better to address the root causes than to patch the effect, in your case there are problems with the positioning of your views and you should address that, foreground/background cycling should not affect the views positioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文