有没有办法防止键盘关闭? [英] Is there a way to prevent the keyboard from dismissing?

查看:31
本文介绍了有没有办法防止键盘关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这与大多数帖子相反,但我希望键盘保持向上即使按下键盘向下"按钮.

I realize that this is the inverse of most posts, but I would like for the keyboard to remain up even if the 'keyboard down' button is pressed.

具体来说,我有一个带有两个 UITextField 的视图.使用以下委托方法

Specifically, I have a view with two UITextFields. With the following delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return NO;
}

即使用户按下键盘上的 Done 按钮或点击屏幕上的任何其他地方,除了键盘右下角那个讨厌的键盘向下按钮,我也能保持键盘打开.

I am able to keep the keyboard up even if the user presses the Done button on the keyboard or taps anywhere else on the screen EXCEPT for that pesky keyboard down button on the bottom right of the keyboard.

我将此视图用作模态视图(尽管该视图与在 UINavigationController 中推送的 ViewController 相关联),因此从用户的角度来看,它确实最有效,可以让键盘始终保持打开状态.如果有人知道如何实现这一点,请告诉我!谢谢!

I am using this view like a modal view (though the view is associated with a ViewController that gets pushed in a UINavigationController), so it really works best from a user perspective to keep the keyboard up all of the time. If anyone knows how to achieve this, please let me know! Thanks!

UPDATE 还是没有解决办法!当按下Done 时,它会触发textFieldShouldReturn,但是当按下Dismiss 按钮时,它会触发textFieldDidEndEditing.我无法阻止 textField 结束编辑或它永远 消失.不知何故,我真的很想拥有一种检测 Dismiss 按钮并忽略它的方法.如果你知道方法,请赐教!

UPDATE Still no solution! When Done is pressed, it triggers textFieldShouldReturn, but when the Dismiss button is pressed, it triggers textFieldDidEndEditing. I cannot block the textField from ending editing or it never goes away. Somehow, I really want to have a method that detects the Dismiss button and ignores it. If you know a way, please enlighten me!

推荐答案

有一种方法可以做到这一点. 因为 UIKeyboard 子类 <代码>UIWindow,唯一能进入UIKeyboard 的东西是另一个UIWindow.

There IS a way to do this. Because UIKeyboard subclasses UIWindow, the only thing big enough to get in UIKeyboard's way is another UIWindow.

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coverKey) name:UIKeyboardDidShowNotification object:nil];
    [super viewDidLoad];
}

- (void)coverKey {
    CGRect r = [[UIScreen mainScreen] bounds];
    UIWindow *myWindow = [[UIWindow alloc] initWithFrame:CGRectMake(r.size.width - 50 , r.size.height - 50, 50, 50)];
    [myWindow setBackgroundColor:[UIColor clearColor]];
    [super.view addSubview:myWindow];
    [myWindow makeKeyAndVisible];
}

这适用于 iPhone 应用.iPad 没试过.您可能需要调整 myWindow 的大小.另外,我没有对 myWindow 进行任何内存管理.所以,也考虑这样做.

This works on iPhone apps. Haven't tried it with iPad. You may need to adjust the size of myWindow. Also, I didn't do any mem management on myWindow. So, consider doing that, too.

这篇关于有没有办法防止键盘关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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