有没有办法防止键盘消失?

发布于 2024-11-07 07:04:41 字数 701 浏览 7 评论 0原文

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

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

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

即使用户按下键盘上的“完成”按钮或点击屏幕上的任何其他位置(除了右下角那个讨厌的键盘向下按钮),我也可以保持键盘处于打开状态。键盘。

我像模态视图一样使用此视图(尽管该视图与推送到 UINavigationController 中的 ViewController 相关联),因此从用户角度来看,始终保持键盘处于打开状态确实效果最佳。如果有人知道如何实现这一目标,请告诉我!谢谢!

更新仍然没有解决方案!当按下Done时,会触发textFieldShouldReturn,但当按下Dismiss按钮时,会触发textFieldDidEndEditing。我无法阻止 textField 结束编辑,否则它永远消失。不知何故,我真的希望有一种方法可以检测 Dismiss 按钮并忽略它。如果您知道方法,请赐教!

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.

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

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

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.

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 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!

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

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

发布评论

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

评论(6

一江春梦 2024-11-14 07:04:41

有一种方法可以做到这一点。因为UIKeyboardUIWindow的子类,所以唯一足够大的东西可以进入< code>UIKeyboard 的方式是另一种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 上进行任何内存管理。所以,也考虑这样做。

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

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.

蘑菇王子 2024-11-14 07:04:41

我想我已经找到了一个很好的解决方案。

添加一个 BOOL 作为实例变量,我们称之为 shouldBeginCalledBeforeHand

然后实现以下方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = YES;
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return shouldBeginCalledBeforeHand;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = NO;
}

以及

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

防止键盘随返回按钮消失。诀窍是,焦点从一个文本字段切换到另一个文本字段将提前触发 textFieldShouldBeginEditing。如果按下关闭键盘按钮,则不会发生这种情况。文本字段获得焦点后,该标志会重置。

I think I've found a good solution.

Add a BOOL as instance variable, let's call it shouldBeginCalledBeforeHand

Then implement the following methods:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = YES;
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return shouldBeginCalledBeforeHand;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = NO;
}

As well as

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

to prevent the keyboard from disappearing with the return button. The trick is, a focus switch from one textfield to another will trigger a textFieldShouldBeginEditing beforehand. If the dismiss keyboard button is pressed this doesn't happen. The flag is reset after a textfield has gotten focus.

救赎№ 2024-11-14 07:04:41

旧的不完美的解决方案

我只能想到一个不完美的解决方案。监听通知 UIKeyboardDidHideNotification 并再次将文本字段设置为第一响应者。这会将键盘移出视线并再次移回。您可以通过监听 UIKeyboardWillHideNotification 来记录哪个文本字段是最后一个firstResponder,并将焦点放在 didHide 中。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidHide:) 
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

...

- (void)keyboardDidHide:(id)sender
{
    [myTextField becomeFirstResponder];
}

Old not perfect solution

I can only think of a not perfect solution. Listen for the notification UIKeyboardDidHideNotification and make of the textfields first responder again. This will move the keyboard out of sight and back again. You could keep record of which textfield was the last firstResponder by listening for UIKeyboardWillHideNotification and put focus on it in the didHide.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidHide:) 
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

...

- (void)keyboardDidHide:(id)sender
{
    [myTextField becomeFirstResponder];
}
渡你暖光 2024-11-14 07:04:41

对于 iOS 9/10 和 Swift 3,使用它创建一个与“隐藏键盘”重叠的矩形 - 按钮

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(coverKey), name: .UIKeyboardDidShow, object: nil)
}

func coverKey() {
    if let keyboardWindow = UIApplication.shared.windows.last {
        let r = UIScreen.main.bounds
        let myWindow = UIWindow.init(frame: CGRect(x: r.size.width - 50 , y: r.size.height - 50, width: 50, height: 50))
        myWindow.backgroundColor = UIColor.clear
        myWindow.isHidden = false
        keyboardWindow.addSubview(myWindow)
        keyboardWindow.bringSubview(toFront: myWindow)
    }
} 

请注意,这会向键盘窗口而不是主窗口添加一个子视图

For iOS 9/10 and Swift 3, use this to create a rect which overlaps the "Hide keyboard" - Button

override func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(coverKey), name: .UIKeyboardDidShow, object: nil)
}

func coverKey() {
    if let keyboardWindow = UIApplication.shared.windows.last {
        let r = UIScreen.main.bounds
        let myWindow = UIWindow.init(frame: CGRect(x: r.size.width - 50 , y: r.size.height - 50, width: 50, height: 50))
        myWindow.backgroundColor = UIColor.clear
        myWindow.isHidden = false
        keyboardWindow.addSubview(myWindow)
        keyboardWindow.bringSubview(toFront: myWindow)
    }
} 

Notice that this adds a sub view to the keyboard window instead of the main window

情域 2024-11-14 07:04:41

尝试在键盘关闭按钮顶部添加自定义,以便用户无法点击关闭按钮。我在我的一个应用程序中使用了这种方法。

- (void)addButtonToKeyboard {

    // create custom button
    UIButton *blockButton = [UIButton buttonWithType:UIButtonTypeCustom];
    blockButton.frame = //set the frame here, I don't remember the exact frame
    [blockButton setImage:[UIImage imageNamed:@"block_button.png"] forState:UIControlStateNormal];

    // locate keyboard view
    UIWindow *appWindows = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i=0; i<[appWindows.subviews count]; i++) {
        keyboard = [appWindows.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && [self.textField isFirstResponder]) {
            [keyboard addSubview:doneButton];

        }
    }

}

Try adding a custom on top of the keyboard dismiss button so that the user won't be able to tab the dismiss button. I have used this method in one of my application.

- (void)addButtonToKeyboard {

    // create custom button
    UIButton *blockButton = [UIButton buttonWithType:UIButtonTypeCustom];
    blockButton.frame = //set the frame here, I don't remember the exact frame
    [blockButton setImage:[UIImage imageNamed:@"block_button.png"] forState:UIControlStateNormal];

    // locate keyboard view
    UIWindow *appWindows = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i=0; i<[appWindows.subviews count]; i++) {
        keyboard = [appWindows.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && [self.textField isFirstResponder]) {
            [keyboard addSubview:doneButton];

        }
    }

}
最后的乘客 2024-11-14 07:04:41

试试这个...

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

您可以使用 Nick Weaver 提到的通知。

Try this...

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

You can use notification as mentioned by Nick Weaver.

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