Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 有没有办法防止键盘失效?_Iphone_Ios_Ipad_Uitextfield_Uikeyboard - Fatal编程技术网

Iphone 有没有办法防止键盘失效?

Iphone 有没有办法防止键盘失效?,iphone,ios,ipad,uitextfield,uikeyboard,Iphone,Ios,Ipad,Uitextfield,Uikeyboard,我意识到这与大多数帖子相反,但我希望即使按下“键盘向下”按钮,键盘仍保持向上 具体来说,我有一个带有两个UITextFields的视图。使用以下委托方法 - (BOOL)textFieldShouldReturn:(UITextField *)textField { return NO; } 即使用户按下键盘上的Done按钮,或点击屏幕上除键盘右下角讨厌的键盘向下按钮之外的任何其他位置,我也能保持键盘向上 我像使用模态视图一样使用这个视图(尽管该视图与在UINavigationCont

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

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return NO;
}
即使用户按下键盘上的
Done
按钮,或点击屏幕上除键盘右下角讨厌的键盘向下按钮之外的任何其他位置,我也能保持键盘向上

我像使用模态视图一样使用这个视图(尽管该视图与在UINavigationController中被推送的ViewController相关联),因此从用户的角度来看,它确实可以最好地保持键盘始终处于打开状态。如果有人知道如何做到这一点,请让我知道!谢谢


更新仍然没有解决方案!当按下
Done
时,它会触发
textfield should return
,但当按下
dismise
按钮时,它会触发
textfield deendediting
。我无法阻止
textField
结束编辑,否则它将永远不会消失。不知何故,我真的很想有一个方法来检测
dismise
按钮并忽略它。如果你知道一个方法,请启发我

我想我找到了一个好办法

添加一个BOOL作为实例变量,我们将其称为
shouldbegincalledbeforeand

然后执行以下方法:

- (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;
}
使用返回按钮防止键盘消失。诀窍是,从一个文本字段到另一个文本字段的焦点切换将触发一个文本字段应该提前开始编辑。如果按下“取消键盘”按钮,则不会发生这种情况。文本字段获得焦点后,标志将重置。

有一种方法可以做到这一点。因为
UIKeyboard
子类
UIWindow
,唯一能够进入
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
上进行任何mem管理。所以,也要考虑这样做。

旧的不完美的解决方案。 我只能想出一个不完美的解决办法。聆听通知
UIKeyboardDidHideNotification
,并再次创建文本字段第一响应者。这会将键盘移到看不见的地方,然后再移回来。您可以通过监听UIKeyboardWillHideNotification并将焦点放在didHide中来记录哪个textfield是最后一个第一响应者

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

...

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

尝试在键盘上添加自定义的“取消”按钮,这样用户就不能在“取消”按钮上添加选项卡。我在我的一个应用程序中使用了此方法

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

        }
    }

}
-(无效)添加按钮键盘{
//创建自定义按钮
UIButton*blockButton=[UIButton Button类型:UIButtonTypeCustom];
blockButton.frame=//在这里设置帧,我不记得确切的帧
[blockButton setImage:[UIImage ImageName:@“block_button.png”]用于状态:UIControlStateNormal];
//定位键盘视图
UIWindow*appWindows=[[[UIApplication sharedApplication]windows]对象索引:1];
UIView*键盘;
对于(inti=0;i试试这个

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

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

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


请注意,这会将子视图添加到键盘窗口,而不是主窗口

@PengOne找到了另一种解决方案。这对于模式视图非常有效,但它会阻止键盘完全关闭,即使视图已关闭:-(你想把这个问题分成两个答案吗?我更喜欢旧的解决方案,因为它虽然不完美,但不会破坏其他任何东西。既然解决方案如此不同,我认为将它们分开是有意义的。@PengOne将答案分开,你能添加一些代码来告诉我你是如何否定模态视图的吗?我认为关于模态视图的问题不是这样的键盘消失是可以解决的。谢谢!这有助于我在使用页面视图控制器时防止键盘消失。我应该补充的是,我假设使用实例字段方法,其中previousTextField和someTextField是实例字段。因此,keyboardWillHide方法不需要参数。尽管这包括“隐藏键盘”按钮,您仍然应该使用UITextField委托方法处理第一响应程序逻辑,以便相应的UITextField接收来自UIKeyboard的输入。我的问题是,这取决于键盘的特定布局/语言。我不反对一般的黑客行为,但这一行为必须针对每个k进行定制eyboard以及不同的方向。您可能只需要创建一个实用程序类,该类根据方向更改、UITextFields之间的焦点更改等触发类方法。类方法将负责添加/删除/修改覆盖键盘特定区域所需的UIWindows。这是一种黑客行为,但可能会e唯一的办法。由于悬赏即将到期,我会在我忘记之前授予它。这个解决方案不是没有问题的,我还是更喜欢一个更好的(因此我不接受这个答案),但在建议的答案中,这是唯一达到预期效果的答案。它绝对值得我的赏赐。感谢JacobB.+1,因为这是一个近乎完美的解决方案。键盘先下后上的事实使这一点不太好。我真希望有一个
-(BOOL)keyboardWillHide
方法,我可以从中返回
。有一个UIKeyboardWillHideNotification,请参阅文档,使用该方法可以让您设置另一个文本字段作为第一响应程序,这应该会取消键盘隐藏/显示动画(但我不确定)。@progrmr尝试过,但没有起作用。也许您已经找到了解决方案
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)
    }
}