使用位于 uiscrollview 顶部的隐形按钮隐藏键盘时遇到问题 [英] Having trouble hiding keyboard using invisible button which sits on top of uiscrollview

查看:15
本文介绍了使用位于 uiscrollview 顶部的隐形按钮隐藏键盘时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个项目在玩...

I have 3 items in play...

1) UIView 位于层次结构的底部,包含 UIScrollview.2) UIScrollview 呈现冗长的用户表单.3) UIScrollview 上的一个隐形按钮,我用来提供隐藏键盘"功能.

1) UIView sits at the base of the hierarchy and contains the UIScrollview. 2) UIScrollview that is presenting a lengthy user form. 3) An invisible button on the UIScrollview that I'm using to provide "hide the keyboard" features.

请注意,在下面的代码中,我正在注册以在键盘出现时收到通知,并在键盘消失时再次收到通知.这些工作得很好.

Notice in the code below that I'm registering to be notified when the keyboard is going to appear and again when it's going to disappear. These are working great.

我的问题似乎是层"之一.请参阅下面我将按钮插入视图 atIndex:0 的位置.这会导致按钮被激活并填充"在滚动视图后面,因此当您单击它时,滚动视图会抓住触摸并且按钮不知道.没有办法触及"按钮并按下键盘.

My problem is seemingly one of "layers". See below where I insert the button into the view atIndex:0. This causes the button to be activated and "stuffed" behind the scrollview so that when you click on it, the scrollview grabs the touch and the button is unaware. There is no way to "reach" the button and suppress the keyboard.

但是,如果我插入 atIndex:1,该按钮将被强加在文本输入字段的顶部,因此任何触摸都由该按钮执行,该按钮立即抑制键盘然后消失.

However, if I insert atIndex:1, the button gets super imposed on top of the text entry fields and so any touch at all is acted upon by the button, which immediately suppresses the keyboard and then disappears.

如何将按钮插入 UIScrollview 的顶部但位于那里的 UITextfields 后面?

How do I insert the button on top of the UIScrollview but behind the UITextfields that sit there?

其他后勤:我有一个 -(void) hidekeyboard 功能,我已将 UIButtion 设置为 IBAction().我通过 ctrl-drag/drop 将 UIButton 连接到文件所有者".(我需要这两个约定吗?)

other logistics: I have a -(void) hidekeyboard function that I have setup with the UIButtion as an IBAction(). And I have the UIButton connected to "files owner" via a ctrl-drag/drop. (Do I need both of those conventions?)

ViewDidLoad() 中的这段代码...

This code in ViewDidLoad()...

[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *notification){

[self.view insertSubview:self.keyboardDismissalButton atIndex:0];

}];

推荐答案

想通了....

这是答案.

下面的代码都在 UIViewController 的 ViewDidLoad() 中.首先从 UIView 中完全删除按钮.(它会在键盘激活时呈现).

The code below is all within the ViewDidLoad() of the UIViewController. Begin by removing the button altogether from the UIView. (it will be rendered when the keyboard is activated).

请注意,在下方的键盘WillShow"通知中,我将按钮插入到我的 UIScrollView 层而不是 UIView 层,就像以前一样.但是,一旦单击按钮并且键盘即将关闭,我就会从 UIView(即 self)中完全删除该按钮.

Notice in the keyboard "WillShow" notification below that I am inserting the button onto my UIScrollView layer rather than the UIView layer, as before. However, once the button is clicked and the keyboard is about to be dismissed, I remove the button altogether from the UIView (i.e. self).

[self.keyboardDismissalButton removeFromSuperview];


[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *notification){

[theScroller insertSubview:self.keyboardDismissalButton atIndex:0];

}];



[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *notification){

        [self.keyboardDismissalButton removeFromSuperview];

}];

这篇关于使用位于 uiscrollview 顶部的隐形按钮隐藏键盘时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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