如何检测UITextView上的水龙头?

时间:2015-03-09 19:08:11

标签: ios swift uitextview uigesturerecognizer

我的观点如下:

当用户尝试输入信息时,键盘会覆盖UITextViews我想要尝试使用我添加到UITextView的{​​{1}}来检测UIGestureRecognizer上的点击,然后根据被点击的视图更改约束。 当我点击它时,应用程序崩溃。

这是我用来检测UITextView -

上点按的代码
UITextViews

我的代码有问题吗?是否有更好的方法来移动//in viewDidLoad let tap = UITapGestureRecognizer(target: self.textYoutube, action: "handleTap") self.textYoutube.addGestureRecognizer(tap) func handleTap(tap: UITapGestureRecognizer) { if (tap.state == UIGestureRecognizerState.Ended) { println("[handleTap] Tap ended") } }

2 个答案:

答案 0 :(得分:0)

使用通知,它们提供键盘框架,新键盘的大小可能会有所不同,框架将允许移动其上方的输入字段。

注册这些通知并处理将视图移出键盘:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

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

从userInfo获取键盘框架:

CGRect keyboarddRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

在dealloc"

中发布通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

或者使用委托方法:

textViewDidBeginEditing:
textViewDidEndEditing:

答案 1 :(得分:0)

检测文本字段上的点击;实现textField的委托方法textFieldShouldBeginEditing:

相关问题